相对布局RelativeLayout详解.docx
《相对布局RelativeLayout详解.docx》由会员分享,可在线阅读,更多相关《相对布局RelativeLayout详解.docx(11页珍藏版)》请在冰点文库上搜索。
相对布局RelativeLayout详解
相对布局:
RelativeLayout
RelativeLayout布局
RelativeLayout顾名思义,相对布局,在这个容器内部的子元素们可以使用彼此之间的位置或则和容器间的相对位置来进行定位。
注意:
不能在RelativeLayout容器本身和他的子元素之间产生依赖,比如说不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的高度设置成为ALIGN_PARENT_BOTTOM.
与RelativeLayout布局相关的几个属性:
控件与控件之间位置相关的:
android:
layout_above将该控件置于给定ID的控件之上。
android:
layout_below将该控件置于给定ID的控件之下。
android:
layout_toLiftOf将该控件置于给定ID的控件之左。
android:
layout_toRightOf将该控件置于给定ID的控件之右。
控件与控件之间对齐相关的:
android:
layout_alignBaseline该控件基线对齐给定ID的基线。
android:
layout_alignBottom该控于给定ID的控件底部对齐。
android:
layout_alignLift该控于给定ID的控件左对齐。
android:
layout_alignRight该控于给定ID的控件右对齐。
android:
layout_alignTop该控于给定ID的控件顶部对齐。
控件和它的容器之间的关系:
android:
layout_alignParentLift如果为True,该控件位于父控件的左部。
android:
layout_alignParentRight如果为True,该控件位于父控件的右部。
android:
layout_alignTop如果为True,该控件位于父控件的顶部。
android:
layout_alignBottom如果为True,该控件位于父控件的底部。
控件和它的容器位置之间的关系:
android:
layout_centerVertical如果为Ture,该控件将被置于水平方向的中央。
android:
layout_centerHorizontal如果为Ture,该控件将被置于垂直方向的中央。
android:
layout_centerInParent如果为Ture,该控件将被置于父控件水平方向和垂直方向。
android="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
>
android:
id="@+id/txt1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="测试RelativeLayout111111111"
android:
background="#aa0000"/>
android:
id="@+id/txt2"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="测试RelativeLayout2222"
android:
background="#0000aa"
android:
layout_below="@id/txt1"111//与txt1(按钮)下方
android:
layout_alignRight="@id/txt1"/>222//与txt1(按钮)右对齐
android="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
>
android:
id="@+id/txt1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_alignParentBottom="true"333//是在底部的时候
android:
text="测试RelativeLayout111111111"
android:
background="#aa0000"/>
android:
id="@+id/txt2"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="测试RelativeLayout2222"
android:
background="#0000aa"
android:
layout_above="@id/txt1"111
android:
layout_alignRight="@id/txt1"/>
android="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
>
android:
id="@+id/txt1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="测试RelativeLayout111111111"
android:
background="#aa0000"
android:
layout_centerInParent="true"/>
android="
xmlns:
tools="
android:
layout_width="fill_parent"
android:
layout_height="wrap_content"
android:
background="#dddd00"
android:
padding="10dp">
android:
id="@+id/textview1"
android:
layout_width="fill_parent"
android:
layout_height="wrap_content"
android:
text="Thememain:
"/>
android:
id="@+id/edittext1"
android:
layout_width="match_parent"
android:
layout_height="wrap_content"
android:
layout_below="@id/textview1"
/>