一.RelativeLayout相对布局方式.
RelativeLayout顾名思义,相对布局,在这个容器内部的子元素们可以使用彼此之间的相对位置或者和容器间的相对位置来进行定位。
注意:不能在RelativeLayout容器本身和他的子元素之间产生循环依赖,比如说,不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的高设置成为 ALIGN_PARENT_BOTTOM。
RelativeLayout相关的布局属性:
android:layout_above 将该控件置于给定ID的控件之上
android:layout_below 将该控件的置于给定ID控件之下
android:layout_toLeftOf 将该控件置于给定ID的控件之左
android:layout_toRightOf 将该控件置于给定ID的控件之右
RelativeLayout布局中相对于父控件来说位置属性:
android:layout_alignParentLeft 如果为True,该控件位于父控件的左部
android:layout_alignParentRight 如果为True,该控件位于父控件的右部
android:layout_alignParentTop 如果为True,该控件位于父控件的顶部
android:layout_alignParentBottom 如果为True,该控件位于父控件的底部
RelativeLayout布局时对齐相关的属性:
android:layout_alignBaseline 该控件基线对齐给定ID的基线
android:layout_alignBottom 该控件于给定ID的控件底部对齐
android:layout_alignLeft 该控件于给定ID的控件左对齐
android:layout_alignRight 该控件于给定ID的控件右对齐
android:layout_alignTop 该控件于给定ID的控件顶对齐
android:layout_centerHorizontal 如果为True,该控件将被置于水平方向的中央
android:layout_centerInParent 如为Ture,该控件将被置于父控件水平方向和垂直方向
android:layout_centerVertical 如果为True,该控件将被置于垂直方向的中央
Relative布局一:效果图:RelativeLayoutOne布局
布局源码:
<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"
android:padding="10px"
android:background="#00aa00"
tools:context=".RelativeLayoutActivity" >
<TextView
android:id="@+id/tv_heard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="12pt"
android:text="XXX系统登录界面" />
<TextView android:id="@+id/tv_account"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/tv_heard"
android:layout_marginTop="30px"
android:text="用户名:"/>
<EditText android:id="@+id/txt_account"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_heard"
android:layout_toRightOf="@id/tv_account"
android:layout_alignBaseline="@id/tv_account"/>
<TextView android:id="@+id/tv_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_account"
android:layout_marginTop="30px"
android:text="密码:"/>
<EditText android:id="@+id/txt_pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt_account"
android:layout_toRightOf="@id/tv_pwd"
android:layout_alignBaseline="@id/tv_pwd"/>
<Button android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_pwd"
android:layout_alignParentRight="true"
android:layout_marginTop="30px"
android:text="退出"/>
<Button android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_pwd"
android:layout_toLeftOf="@id/btn_exit"
android:layout_marginTop="30px"
android:layout_marginRight="50px"
android:text="登录"/>
</RelativeLayout>
RelativeLayoutSecond布局效果图:使用 LinearLayout和 RelativeLayout结合布局更加快捷方便。
布局源码:
<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"
android:background="#00aaff"
android:padding="10px"
tools:context=".RelativeLayoutSecondActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="12pt"
android:text="XXX系统登录界面" />
<LinearLayout android:id="@+id/linear_one"
android:layout_below="@id/tv_heard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="用户名:"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="@+id/linear_second"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_below="@id/linear_one">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditTextandroid:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="@+id/linear_third"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_below="@id/linear_second"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30px"
android:text="退出"/>
</LinearLayout>
</RelativeLayout>