【问题标题】:Have a button floating in front of a List View在列表视图前浮动一个按钮
【发布时间】:2020-12-04 12:43:44
【问题描述】:

我希望在片段中的可滚动 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 设计视图“理解”我正在尝试执行的操作,但是当我运行应用程序时,只会出现“新建项目”按钮,或者什么也不会出现。

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

谢谢,

亚历克斯

【问题讨论】:

  • 只在你的列表和浮动按钮中创建一个带有约束视图组的布局,仅此而已

标签: android android-layout android-listview android-view android-button


【解决方案1】:

使用按钮将ListView 移动到RelativeLayout 上方。布局中的顶视图是布局底部的视图,因此将列表视图移动到布局文件的顶部使其成为底视图

【讨论】:

    【解决方案2】:

    你说的好

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

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

    但是,有很多方法可以在ListView 之上实现FAB。最简单的一个 使用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>
    

    编码愉快!

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2021-06-15
      相关资源
      最近更新 更多