【问题标题】:Floating Action Button gravity not working浮动操作按钮重力不起作用
【发布时间】:2017-02-12 15:31:49
【问题描述】:
我有一个浮动操作按钮,应该在右下角,但显然重力不起作用。它显示在左上角。这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/rl"
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">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@mipmap/ic_action_send"
app:backgroundTint="@color/colorPrimaryDark"
app:layout_anchor="@id/iv"
app:layout_anchorGravity="bottom|right|end"/>
</RelativeLayout>
【问题讨论】:
标签:
android
xml
floating-action-button
【解决方案1】:
我有一个浮动操作按钮,应该在右下角
角落,但显然重力不起作用
这是预期的行为,除非您将内容包装在 CoordinatorLayout 周围。例如
<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"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@mipmap/ic_action_send"
app:backgroundTint="@color/colorPrimaryDark"
app:layout_anchor="@id/iv"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
应该这样做。你可以阅读更多关于CoordinatorLayouthere
【解决方案2】:
使用android:layout_alignParentBottom="true" 和android:layout_alignRight="@id/iv" 作为 FloatingActionButton 参数。
【解决方案3】:
使用android:layout_gravity="bottom|end"而不是app:layout_anchorGravity="bottom|right|end"将其放入末端底部(右下)角
【解决方案4】:
对于FrameLayout,你可以试试:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="ch13mob.wifiofworldairports.fragment.FragmentWifiAirports">
<include layout="@layout/wifi_list" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_wifi_black_24dp" />
</FrameLayout>