如何让android支持屏幕右下角的浮动操作按钮?
我在如何让android支持屏幕右下角的浮动操作按钮?,android,xml,Android,Xml,我在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="fil
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
,但我的floatingAction按钮的位置没有更改,它位于左上角
如何在右下角创建我的FloatingActionButton
?既然您正在使用RelativeLayout包装您的FloatingActionButton,请改用以下属性:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
希望这有帮助。使用协调器布局:
<?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>
从代码中清除:
android:layout_gravity=“bottom | right”
添加此行:
app:layout_anchorgavity=“bottom | right | end”
从代码中清除:
android:layout_gravity=“底部|右侧”
添加以下内容:
app:layout_anchorGravity=“bottom | right | end”协调人布局是为了应对此类情况。如果我是在你@DäñishShärmá中,我会坚持它。我已经用你的答案编辑了我的代码,并且我对你的答案进行了升级。还有一件事我想问一下如何改变浮动按钮的背景色。我想让它和我的原色一样深。可能吗?如果您有,请删除app:backgroundTint=“#FF0000”
。默认情况下,如果主题设置正确,则应获得它
app:layout_anchor="@id/test"
app:layout_anchorGravity="bottom|right|end"