RelativeLayout(相对布局)
RelativeLayout允许子元素指定他们相对于其它元素或父元素的位置(通过ID指定)。因此,你可以以右对齐或上下,或置于屏幕中央的形式来排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用XML来指定这个layout,在你定义它之前,被关联的元素必须定义。
通过一个简单的实例看看相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="55dp"
android:layout_height="50dp"
android:background="#b3b3b3" />
<TextView
android:id="@+id/text2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/ic_launcher"
android:layout_alignTop="@id/text1"
android:layout_toRightOf="@id/text1"
/>
<TextView
android:id="@+id/text3"
android:layout_width="55dp"
android:layout_height="50dp"
android:background="#b3b3b3"
android:layout_alignTop="@id/text2"
android:layout_toRightOf="@id/text2"/>
<TextView
android:id="@+id/text4"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/ic_launcher"
android:layout_alignTop="@id/text3"
android:layout_toRightOf="@id/text3"
/>
<TextView
android:id="@+id/text5"
android:layout_width="55dp"
android:layout_height="50dp"
android:background="#b3b3b3"
android:layout_alignTop="@id/text4"
android:layout_toRightOf="@id/text4"/>
<TextView
android:id="@+id/text6"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/ic_launcher"
android:layout_alignTop="@id/text5"
android:layout_toRightOf="@id/text5"
/>
<TextView
android:id="@+id/text7"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#b3b3b3"
android:layout_below="@id/text1"
android:layout_marginTop="5dp"
/>
</RelativeLayout>
代码说明:
android:background="#b3b3b3" 设置TextView的背景色
android:layout_alignTop="@id/text1" 设置 TextView顶端对齐
android:layout_toRightOf="@id/text1" 设置 TextView居右显示
android:layout_below="@id/text1" 设置 TextView在下面显示
android:layout_marginTop="5dp" 设置 TextView在上缝隙的间距
android:background="@drawable/ic_launcher" 设置TextView的背景图片
结果如图:
RelativeLayout用到的一些重要的属性:
第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
用途实例:用相对布局出的文件可以说是不受屏幕分辨率和尺寸的影响,因此用途非常之广。