在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局。但在实际开发中使用LinearLayout远远不够,我们本期一起来学习RelativeLayout。
一、认识RelativeLayout
RelativeLayout,又叫相对布局,使用<RelativeLayout>标签。相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。
下表显示了RelativeLayout支持的常用XML属性及相关方法的说明。
XML属性 | 相关方法 | 说明 |
---|---|---|
android:gravity | setGravity(int) | 设置该布局容器内各子组件的对其方式 |
android:ignoreGravity | setIgnoreGravity(int) | 设置哪个组件不受gravity属性的影响 |
为了控制该布局容器中各子组件的布局分布,RelativeLayout提供了一个内部类: RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局容器中子组件的布局分布。
在相对于容器定位的属性主要有以下几个,属性值为true或false。
在相对于其他组件定位的属性主要有以下几个,属性值为其他组件的id。
除此之外,RelativeLayout.LayoutParams 还继承了 android view. ViewGroup.MarginLayoutParams,因此 RelativeLayout 布局容器中每个子组件也可指定 android.view.ViewGroiip.MarginLayoutParams所支持的各XML属性。
二、示例
接下来通过一个简单的示例程序来学习RelativeLayout的使用用法。
继续使用app/main/res/layout/目录下的activity_main.xml文件,在其中填充如下代码片段:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义该组件位于父容器左上侧 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器左上侧"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<!-- 定义该组件位于父容器上侧水平居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器上侧水平居中"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<!-- 定义该组件位于父容器右上侧 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器右上侧"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<!-- 定义该组件位于父容器左侧垂直居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左中"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<!-- 定义该组件位于父容器右侧垂直居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右中"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<!-- 定义该组件位于父容器左下侧 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器左下侧"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
<!-- 定义该组件位于父容器下侧水平居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器下侧水平居中"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<!-- 定义该组件位于父容器右下侧 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器右下侧"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<!-- 定义该组件位于父容器中间 -->
<Button
android:id="@+id/center_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器中央"
android:layout_centerInParent="true"/>
<!-- 定义该组件位于center_btn组件的上方 -->
<Button
android:id="@+id/center_top_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中上"
android:layout_above="@id/center_btn"
android:layout_alignLeft="@id/center_btn"/>
<!-- 定义该组件位于center_btn组件的下方 -->
<Button
android:id="@+id/center_bottom_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中下"
android:layout_below="@id/center_btn"
android:layout_alignLeft="@id/center_btn"/>
<!-- 定义该组件位于center_btn组件的左边 -->
<Button
android:id="@+id/center_bottom_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中下左"
android:layout_toLeftOf="@id/center_btn"
android:layout_alignTop="@id/center_bottom_btn"/>
<!-- 定义该组件位于center_btn组件的右边 -->
<Button
android:id="@+id/center_top_right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中上右"
android:layout_toRightOf="@id/center_btn"
android:layout_alignTop="@id/center_top_btn"/>
</RelativeLayout>
运行程序,可以看到下图所示界面效果:
到此,RelativeLayout的示例结束,关于RelativeLayout的更多用法可以参照上面的XML属性和方法参照表,建议多动手练习。