特点:按组件的相对位置来多布局,如某个组件在某个组件的上下左右。
常用属性:
| XML属性 | 描述 |
| android:layout_above | 指定该组件在某个组件上方 |
| android:layout_below | 指定该组件在某个组件下方 |
| android:layout_toLeftOf | 指定该组件在某个组件左边 |
| android:layout_toRightOf | 指定该组件在某个组件的右边 |
| android:layout_centerInParent | 指定该组件在父组件的中间 |
| android:layout_centerVertical | 垂直居中 |
| android:layout_centralHorizontal | 水平居中 |
| android:layout_alignRight | 指定这个组件和哪个组件左边对齐 |
| android:layout_alignLet | 指定这个组件和哪个组件右边对齐 |
| android:layout_alignBottom | 指定这个组件和哪个组件底端对齐 |
| android:layout_alignTop | 指定这个组件和哪个组件顶端对齐 |
| android:layout_alignParentRight | 用于指定该组件是否与父控件右端对齐 |
| android:layout_alignParentLeft | 指定该组件是否与父控件左端对齐 |
| android:layout_alignParentTop | 指定该组件是否与父控件顶端对齐 |
| android:layout_alignParentDown | 指定该组件是否与父控件下面对齐 |
<?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" ><Buttonandroid:id="@+id/btn_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="中" /><Buttonandroid:id="@+id/btn_up"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/btn_center"android:layout_alignLeft="@+id/btn_center"android:text="上" /><Buttonandroid:id="@+id/btn_down"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/btn_center"android:layout_centerHorizontal="true"android:text="下" /><Buttonandroid:id="@+id/btn_left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toLeftOf="@+id/btn_center"android:text="左" /><Buttonandroid:id="@+id/btn_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/btn_center"android:layout_toRightOf="@+id/btn_center"android:text="右" /></RelativeLayout>
<?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" ><Buttonandroid:id="@+id/btn_center1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="中" /><Buttonandroid:id="@+id/btn_left1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/btn_center1"android:text="左" /><Buttonandroid:id="@+id/btn_right1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/btn_left1"android:layout_alignParentRight="true"android:text="右" /><Buttonandroid:id="@+id/btn_left2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/btn_center1"android:text="左" /><Buttonandroid:id="@+id/btn_right2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/btn_left2"android:layout_alignParentRight="true"android:text="右" /></RelativeLayout>
本文详细介绍了在布局组件时使用相对位置属性的方法,包括如何通过XML属性指定组件之间的上下左右关系,以及如何实现垂直和水平居中、对齐等布局效果。通过示例代码演示了具体应用。
4464

被折叠的 条评论
为什么被折叠?



