我一直在为FAB的位置而苦苦挣扎,由于某种未知的原因(对于我来说,此按钮位于左下角,当它位于底部时,它位于左上角。
这是我的代码:
<RelativeLayout 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:background="@color/l_gray"
android:weightSum="1">
<ListView
android:id="@+id/lv_vehiculos"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:divider="@color/transparent"
android:dividerHeight="15dp"
android:layout_weight="0.83"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:layout_marginBottom="0dp"
android:clickable="true"
android:src="@drawable/icon_white_plus"
app:backgroundTint="@color/s_gray"
app:borderWidth="0dp"
app:elevation="2dp"
android:id="@+id/view" />
这就是结果。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9SQ2hrQi5wbmcifQ==” alt =“ FAB错放了”>
如果您将RelativeLayout
用于父级,则应使用以下内容将其发送到视图的右下角:
android:alignParentRight="true"
android:alignParentEnd="true"
android:alignParentBottom="true"
相对布局及其子级不能使用布局权重和weightsum属性及其行为。同样,相对布局的直接子级也没有布局重力属性,因此您期望从这些属性获得的任何行为都将不起作用
因此,为了使晶圆厂右下角,您需要使用相对布局内可用的定位属性,因此,如果要通过将FAB的值设置为android:layout_alignParentBottom
来启用FAB的true
属性,则该按钮将移到底部,然后如果还通过将FAB设置为android:layout_alignParentRight
来启用FAB的true
属性,则该按钮将粘在父级相对布局的右端,因此您已成功将按钮定位到了您的相对布局(父母)。
但是,在您的情况下,一个更简单的解决方案是将根相对布局更改为linearlayout,就像我在xml中所做的那样,很明显,您的根布局的子级是为linearlayout父级设置的,并且您天真地复制了粘贴内容将该代码转换为具有根相对布局的布局
您的版式上存在一些问题。
android:layout_weight
不适用于RelativeLayout
。您可以检查official doc here.
android:layout_gravity
不适用于RelativeLayout
。您可以检查official doc here.
您可以通过多种方式实现布局:
CoordinatorLayout
和CoordinatorLayout
。并且可以使用android:layout_gravity="end|bottom"
代替RecyclerView
。ListView
代替LinearLayout
RelativeLayout
与RelativeLayout
和android:alignParentEnd="true"
一起使用android:alignParentBottom="true"