这是我参与 8 月更文挑战的第 28 天,活动详情查看: 8月更文挑战
简介
RelativeLayout就是相对布局. 相对于LinearLayout的线性布局而言更具灵活性, LinearLayout仅支持水平或者垂直排列. RelativeLayout可以通过相对定位的方式让控件出现在布局的任何位置, 因此RelativeLayout的属性也非常多, 不过具有很强的规律性.本篇简单介绍RelativeLayout基于控件的属性.
使用
相对父布局对其
注意, RelativeLayout内的所有控件默认初始位置为0.0即左上角, 如果不指定控件位置, 则所有的控件都会在左上角堆积,显示在最顶部的控件为xml布局中的最下边的控件
<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=".RelativeLayoutActivity">
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="居中" />
<Button
android:id="@+id/btn_left"
android:layout_toLeftOf="@id/btn_center"
android:layout_above="@id/btn_center"
android:text="左上"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="右上"
android:id="@+id/btn_right"
android:layout_above="@id/btn_center"
android:layout_toRightOf="@id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="左下"
android:id="@+id/btn_left_bottom"
android:layout_below="@id/btn_center"
android:layout_toLeftOf="@id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="右下"
android:id="@+id/btn_right_bottom"
android:layout_toRightOf="@id/btn_center"
android:layout_below="@id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
基本属性
android:layout_toStartOf 是否同指定布局的开始位置对其
android:layout_toLeftOf 是否同指定布局的左边对其,其效果同android:layout_toStartOf(推荐使用)
android:layout_toEndOf 是否同指定布局的结束位置对其
android:layout_toRightOf 是否同指定布局的右边对其,其效果同android:layout_toEndOf(推荐使用)
android:layout_below是否同指定布局的底部对其
android:layout_above是否同指定布局的顶部对其
layout_alignTop和指定布局的上边框对其
layout_alignBottom和指定布局的下边框对其
layout_alignLeft和指定布局的左边框对其
layout_alignStart和指定布局的左边框对其(推荐使用)
layout_alignRight和指定布局的右边框对其
layout_alignEnd和指定布局的右边框对其(推荐使用) \