【发布时间】: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