[笔记]android UI ------ RelativeLayout
RelativeLayout是一个在相对位置上显示子View元素的VeiwGroup,一个视图的位置,可以指定为相对于兄妹的元素(比如一个给定的与孙的左边或者下边)或者心爱那个对于RelativeLayout区域的位置(比如与底部对齐,剩下的中心)
一个RelativeLayout是一个非常强大使用的为设置用户界面的布局,因为它可以消除嵌套的视图组ViewGroup,如过你发现你用了几个嵌套的LinearLayout组,你可以替换为一个单独的RelativeLayout
1、开始一个新的工程,名字叫做HelloRelativeLayout
2、打开res/layout/main.xml文件并且插入如下信息
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/label" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="Type here:" 12 /> 13 <EditText 14 android:id="@+id/entry" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:background="@android:drawable/editbox_background" 18 android:layout_below="@id/label" 19 /> 20 <Button 21 android:id="@+id/ok" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_below="@id/entry" 25 android:layout_alignParentRight="true" 26 android:layout_marginLeft="10dip" 27 android:text="OK" 28 /> 29 <Button 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_toLeftOf="@id/ok" 33 android:layout_alignTop="@id/ok" 34 android:text="Cancel" 35 /> 36 </RelativeLayout>
3、注意到每一个android:layout_*属性,比如layout_below,layout_alignParentRightRight,和layout_toLeftOf,当用一个RelativeLayout的时候,你可以用这些属性来描述你想要的每个视图View的位置,每一个这些属性都定义一个不懂种类的相对位置,一些属性用到同级视图的资源ID来定义自己的相对位置。比如最后一个Button是被定义到位于被定义ID为ok的视图的左边和顶部对齐,所有的layout布局属性都被定义在RelativeLayout.LayoutParams中

浙公网安备 33010602011771号