[英]Floating Action Button gravity not working
I have a floating action button which should be in the bottom right corner, but apparently the gravity isn't working.我有一个应该在右下角的浮动操作按钮,但显然重力不起作用。 It is shown in the top left corner.
它显示在左上角。 Here is my code:
这是我的代码:
<?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>
I have a floating action button which should be in the bottom right corner, but apparently the gravity isn't working
我有一个浮动动作按钮应位于右下角,但显然重力不起作用
it is the expected behavior unless you wrap your content around a CoordinatorLayout
. 除非您将内容包装在
CoordinatorLayout
周围,否则这是预期的行为。 Eg 例如
<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>
should do it. 应该这样做。 You can read more about the
CoordinatorLayout
here 您可以在此处阅读有关
CoordinatorLayout
更多信息
使用android:layout_alignParentBottom="true"
和android:layout_alignRight="@id/iv"
作为FloatingActionButton参数。
使用android:layout_gravity="bottom|end"
而不是app:layout_anchorGravity="bottom|right|end"
将它放入最后的底部(右下角)
For FrameLayout, you can try: 对于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>
in case you're using RelativeLayout如果您使用的是 RelativeLayout
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/arrow_back"
android:contentDescription="Help"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16dp" />
use采用
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.