我在 RelativeLayout
内的布局中添加了一个 FloatingActionButton
,如下所示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_ok" />
</RelativeLayout>
如您所见,我已将 layout_gravity
设置为 bottom|right
但我的 FloatingActionButton
的位置没有改变,它位于左上角。
我怎样才能让我的 FloatingActionButton
在右下角?
最佳答案
使用 CoordinatorLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent">
<RelativeLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:layout_margin="10dp"
android:id="@+id/myFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#FF0000"
app:borderWidth="0dp"
app:elevation="8dp"
app:layout_anchor="@id/test"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
注意
app:layout_anchor="@id/test"
app:layout_anchorGravity="bottom|right|end"
关于android - 如何让android支持屏幕右下角的FloatingActionButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088453/