天天看点

Android——布局中bottom不起作用/left、right不起作用

LinearLayout中,如果设置位水平排列,只能在垂直方向上起作用(top、buttom等)

如果设置位垂直排列,只能在水平方向上起作用(left、right等)

想要这些都能同时起作用,可以使用FrameLayout。

下面是我做的一个FAB的布局

<FrameLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".FloatButtonActivity">
       <com.google.android.material.floatingactionbutton.FloatingActionButton
           android:id="@+id/float_button"
           android:layout_gravity="bottom|right"
           android:layout_margin="50dp"
           android:elevation="5dp"
           android:layout_width="wrap_content"
           android:src="@mipmap/FAB_add"
           android:fabSize="mini"
           android:layout_height="wrap_content"/>
</FrameLayout>
           

效果图

Android——布局中bottom不起作用/left、right不起作用