如何为RelativeLayout内底部对齐父布局的按钮添加底部边距
问题原因
你当前使用的根RelativeLayout的宽高属性设置为wrap_content,父布局的尺寸会刚好适配内部子控件的大小,没有多余的留白空间。android:layout_marginBottom是子控件相对于父布局底部的外边距,此时父布局底部与按钮底部完全重合,20dp的边距没有可渲染的空间,所以看起来没有生效。
解决步骤
- 修改根
RelativeLayout的android:layout_width和android:layout_height属性为match_parent,让父布局撑满整个屏幕可用空间,此时设置的边距会相对于屏幕边缘生效 - 可删除悬浮按钮上冗余的
android:paddingBottom="20dp"属性,该属性是按钮内部的内边距,不会调整按钮在布局中的位置
修正后的完整XML代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".MainActivity"> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/btn_add_review" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginEnd="20dp" android:layout_marginBottom="20dp" android:clickable="true" app:backgroundTint="@color/themeColorOrange" app:srcCompat="@drawable/ic_baseline_add_24" /> </RelativeLayout>
内容的提问来源于stack exchange,提问作者Ali K
相关产品推荐
相关产品推荐

