有一个按钮浮动在列表视图的前面

问题描述

我想让一个按钮永久悬浮在片段中可滚动ListView的顶部。我曾尝试使用FrameLayouts,LinearLayout,RelativeLayout等,但没有任何效果

这是我正在处理的基本代码

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--        New Project Button-->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_margin="5dp"
    android:id="@+id/fragment_profile_project_relLayoutButton">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_profile_project_newProject_button"
        android:layout_alignParentRight="true"
        android:paddingHorizontal="15dp"
        android:text="@string/project_new_project_button"
        android:background="@drawable/white_rounded_button"/>
</RelativeLayout>

<!--        List View-->
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_profile_project_listView"
    android:layout_marginBottom="50dp"/>

Android Studio设计视图“了解”我正在尝试做的事情,但是当我运行该应用程序时,仅出现“新建项目”按钮,否则什么也没有。

enter image description here

有人知道如何在ListView前面获得一个浮动按钮吗?

谢谢

亚历克斯

解决方法

使用按钮将let contentView = ContentView().environmentObject(UserSettings.sharedInstance)移到ListView上方。布局中的顶视图是布局底部的视图,因此将listview移到布局文件的顶部会使其成为底视图

,

你说

Android Studio设计视图“了解”,但是仅当我运行该应用程序时 出现“新项目”按钮,或者什么也没有

可能需要检查列表视图设置的代码。

但是,有很多方法可以在FAB之上实现ListView。最简单的一个 如下使用FrameLayout-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ..................................../>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_margin="16dp"
        android:layout_gravity="end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ..................................../>

</FrameLayout>

快乐编码!