13 - 布局Layout:RelativeLayout相对布局

来源:互联网 发布:linux iconv命令 编辑:程序博客网 时间:2024/06/05 17:15
转载注明出处:http://blog.csdn.net/eana_don/article/details/8364144
资料参考:官网API

Relative Layout是相对布局。每一个控件都是有着相对的位置。这个布局可以减少layout的嵌套。在布局没有那么规律控件又较多的时候,方便使用。


一、Relative Layout的常用属性:

指定放置于给定ID控件的上/下/左/右android:layout_above 将该控件的底部置于给定ID的控件之上android:layout_below 将该控件的底部置于给定ID的控件之下android:layout_toLeftOf将该控件的右边缘与给定ID的控件左边缘对齐android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐指定与给定ID控件的对齐方式android:layout_alignBaseline将该控件的baseline与给定ID的baseline对齐指定了之后在指定居中属性,
居中属性不生效android:layout_alignTop将该控件的顶部边缘与给定ID的顶部边缘对齐android:layout_alignBottom将该控件的底部边缘与给定ID的底部边缘对齐android:layout_alignLeft将该控件的左边缘与给定ID的左边缘对齐android:layout_alignRight将该控件的右边缘与给定ID的右边缘对齐指定与父控件的对齐方式android:layout_alignParentTop 如果为true,将该控件的顶部与其父控件的顶部对齐android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐android:layout_alignParentLeft 如果为true,将该控件的左部与其父控件的左部对齐android:layout_alignParentRight 如果为true,将该控件的右部与其父控件的右部对齐指定居中属性android:layout_centerHorizontal如果为true,将该控件的置于水平居中android:layout_centerVertical如果为true,将该控件的置于垂直居中android:layout_centerInParent如果为true,将该控件的置于父控件的中央

二、xml布局写法:(来自官网demo)


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingLeft="16dp"    android:paddingRight="16dp" >    <EditText        android:id="@+id/name"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="Reminder Name" />    <Spinner        android:id="@+id/dates"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_below="@id/name"        android:layout_alignParentLeft="true"        android:layout_toLeftOf="@+id/times" />    <Spinner        android:id="@id/times"        android:layout_width="96dp"        android:layout_height="wrap_content"        android:layout_below="@id/name"        android:layout_alignParentRight="true" />    <Button        android:layout_width="96dp"        android:layout_height="wrap_content"        android:layout_below="@id/times"        android:layout_alignParentRight="true"        android:text="Done" /></RelativeLayout>


三、运行结果:


原创粉丝点击