7.3 RelativeLayout格局详解
7.3 RelativeLayout布局详解
RelativeLayout相对布局, 允许子元素指定他们相对于其它元素或父元素的位置(通过ID 指定)。因此,可以以左右对齐、上下对齐、置于屏幕*等形式来排列元素。相对布局在实际应用中比较常用。图7-13所示是垂直方向上的应用。
图7-13 RelativeLayout1
RelativeLayout布局文件请参考代码清单7-15,完整代码请参考chapter7_3工程中relativelayout1.xml代码部分(chapter7_3/res/layout/relativelayout1.xml)。
【代码清单7-15】
图7-14所示是水平方向上的应用。
图7-14 RelativeLayout2
RelativeLayout布局文件请参考代码清单7-16,完整代码请参考chapter7_3工程中relativelayout2.xml代码部分(chapter7_3/res/layout/relativelayout2.xml)。
【代码清单7-16】
出自《Android开发案例驱动教程》第七章
RelativeLayout相对布局, 允许子元素指定他们相对于其它元素或父元素的位置(通过ID 指定)。因此,可以以左右对齐、上下对齐、置于屏幕*等形式来排列元素。相对布局在实际应用中比较常用。图7-13所示是垂直方向上的应用。
图7-13 RelativeLayout1
RelativeLayout布局文件请参考代码清单7-15,完整代码请参考chapter7_3工程中relativelayout1.xml代码部分(chapter7_3/res/layout/relativelayout1.xml)。
【代码清单7-15】
<?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="wrap_content" android:padding="10dip"> <TextView android:id="@+id/labe1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="上" android:background="#ff0060" /> <TextView android:id="@+id/labe2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="下" android:background="#2121f4" /> <TextView android:id="@+id/labe3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/labe2" android:layout_below="@id/labe1" android:text="中" android:background="#ffffff" /> </RelativeLayout>
图7-14所示是水平方向上的应用。
图7-14 RelativeLayout2
RelativeLayout布局文件请参考代码清单7-16,完整代码请参考chapter7_3工程中relativelayout2.xml代码部分(chapter7_3/res/layout/relativelayout2.xml)。
【代码清单7-16】
<?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="wrap_content" android:padding="10dip"> <TextView android:id="@+id/labe4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/search" /> <EditText android:id="@+id/entry" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/labe4" android:background="@android:drawable/editbox_background" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="@string/confirm" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="@string/cancel" /> </RelativeLayout>
出自《Android开发案例驱动教程》第七章