特征:
1. 每个控件都可以使用其它控件作为参考点,从而决定自身的尺寸或者位置
2. 必须首先确定1个不以其它控件作为参考点,而是可以直接确定自身的尺寸和位置的控件
3. 当控件没有使用参考点控件时,默认以根布局作为参考点,且靠左对齐,靠上对齐
4. 相对布局中的子级布局控件可以重叠
属性:特有属性: align系列 --> 将控件设置为与相应控件的左右上下对齐
to 系列 --> 将控件设置在相应控件的 上下左右
相对布局的子级控件的属性:
1. android:layout_alignParentLeft -> 对齐父级控件的左侧边缘,取值为true或者false
2. android:layout_alignParentRight -> 对齐父级控件的右侧边缘,取值同上
3. android:layout_alignParentTop -> 对齐父级控件的顶部边缘,取值同上
4. android:layout_alignParentBottom -> 对齐父级控件的底部边缘,取值同上
5. android:layout_centerHorizontal -> 在父级控件范围内水平方向居中对齐,取值同上
6. android:layout_centerVertical -> 在父级控件范围内容垂直方向居中对齐,取值同上
7. android:layout_centerInParent -> 在父级控件范围内水平方向和垂直方向均居中对齐,取值同上
8. android:layout_alignTop -> 与其它某个控件的顶部在垂直方向对齐,取值为其它控件的id
9. android:layout_alignBottom -> 与其它某个控件的底部在垂直方向对齐,取值同上
8. android:layout_alignLeft -> 与其它某个控件的左侧在水平方向对齐,取值同上
9. android:layout_alignRight -> 与其它某个控件的右侧在水平方向对齐,取值同上
10. android:layout_toLeftOf -> 设置控件在水平方向的位置在其它某个控件的左侧,取值为其它控件的id
11. android:layout_toRightOf -> 设置控件在水平方向的位置在其它某个控件的右侧,取值同上
12. android:layout_above -> 设置控件在垂直方向的位置在其它某个控件的上方,取值同上
13. android:layout_below -> 设置控件在垂直方向的位置在其它某个控件的下方,取值同上
Ps:
1. 当以其它控件作为参考点,其它控件必须与自身是同级(拥有相同的直接父级控件)控件
2. 不可以互相参考
相对布局
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="51dp"
android:layout_marginTop="21dp"
android:text="相对布局" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="96dp"
android:src="@drawable/ic_launcher" />
2)LinearLayout (线性布局)
特征:
1. 直接子级布局的控件按照代码的顺序先后排列
2. 直接子级布局的控件可以按照比例分配尺寸
属性:特有属性: weight --> 将宽(高)设置为0dp,则设置weight可按照比例进行分配空间
orientation --> 设置内部空间按垂直(水平)排布
android:orientation -> 布局方向,取值为vertical或者horizontal,前者表示子级布局的控件垂直方向(从上至下)排列,后者表示子级布局的控件水平方向(从左至右)排列
Ps:线性布局的直接子级布局的控件可以配置如下属性:
android:layout_weight -> 权重/比例,取值为整数
android:layout_gravity -> 对齐方式,取值为left、right、top、bottom、center,或者其中2个不冲突的值组合起来,例如top|left
垂直布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >//垂直布局
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
水平布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
3)GridLayout (网格布局)
android:layout_row=”0”表示从第一行开始,
android:layout_column=”0”表示从第一列开始
column --> 跨列
rowSpan --> 跨行
4)FrameLayout(帧布局)
FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。
5) AbsoluteLayout绝对布局
AbsoluteLayout绝对布局犹如div指定了absolute属性,用X,Y坐标来指定元素的位置!
这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差