【问题标题】:Floating button over bottom sheet layout底部工作表布局上的浮动按钮
【发布时间】:2020-06-29 20:10:14
【问题描述】:

我想设计一个底部工作表,它有一个 TextView(“删除”)和一个浮动按钮('x'),如图所示:

如何实现这一点,以便“x”出现在 TextView 布局上?我尝试了不同的变化,但它们没有产生我想要的结果。我不确定我错过了什么。我在网上发现了很多在布局上使用 FloatingButton 的文章,但由于某种原因,我的输出总是出乎意料。我在stackoverflow上也发现了一个类似的问题,但是答案不是很有帮助:link

我的 bottom_sheet.xml 布局代码如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/delete_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background_primary">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Delete"
            android:textColor="@color/text_primary"
            android:textSize="15sp" />
    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="24dp"
        android:layout_height="24dp"
        app:layout_anchor="@id/delete_container"
        android:layout_gravity="top|end"
        android:src="@drawable/ic_cancel_24dp" />

</FrameLayout>

但这只是将浮动按钮添加到 LinearLayout 的末尾(右侧)。

我还尝试在我的 LinearLayout 中添加一个 marginTop,以便它向下“推”布局并在顶部为按钮留出空间,但随后该空间仅被白色背景色填充。我还尝试将 alpha = 0 添加到最外面的 FrameLayout 以避免这种情况,但它仍然显示为白色背景。

如果有任何意见,我将不胜感激。感谢您的宝贵时间。

【问题讨论】:

  • 必须是FrameLayout吗?我使用 ConstraintLayouts 并将 FAB 锚定在屏幕的右侧和底部,它只是浮​​动在其他所有内容之上。
  • 或 relativeLayout....
  • android:layout_gravity="top|end" 更改为app:layout_anchorGravity="top|end" 或使用ConstraintLayout 以方便定位。

标签: android


【解决方案1】:

一个带有 ConstraintLayout 的工作示例(这看起来要简单得多):

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ...>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
    ... />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 谢谢@einusername。这是否应该在它自己的 xml 布局文件中?我只是想知道如何将它添加到底部显示“删除”的表格上方。
  • 不,它与您的 xml 文件相同。只需将根布局替换为单个 ConstraintLayout。无缘无故将它们相互嵌套是不好的。 app:layout_constraintBottom_toTopOf="@+id/deleteTextViewId" 会将其保持在 textView 上方
  • 这行得通,但是,我看到 textview 上方的白色背景颜色是出乎意料的。看起来像这样:imgur.com/a/3JUqshI
  • 你是把蓝色背景色放在根ConstraintLayout上还是还是在不同的布局中?
  • 如果 android:background=blue 在 TextView 上,则文本视图上方的按钮会出现白色背景。如果 android:background 位于外部 ConstraintLayout 上,则按钮和 textview 都具有该背景。我只是想让按钮根本没有任何背景,只有 TextView 有蓝色背景。
猜你喜欢
  • 1970-01-01
  • 2018-02-14
  • 2020-07-19
  • 1970-01-01
  • 2018-01-21
  • 2015-08-26
  • 1970-01-01
  • 2016-11-22
  • 2016-10-05
相关资源
最近更新 更多