1.基本属性
- android:layout_width:用来设置布局宽度。
- android:layout_height:用来设置布局高度。以上两个属性在布局管理器中一般设置为match_parent,即与父容器相同。
- android:paddingBotton:设置底内边距。 android:paddingLeft:设置左内边距。
- android:paddingRight:设置右内边距。
- android:paddingTop:设置顶内边距。以上四个属性可以设置为具体数值,比如16dp;也可以使用尺寸资源进行定义
- layout_alignParentBottom:父组件底部一条线,也就是并齐。
- layout_toRightOf:定义在谁的右边,紧紧相邻。
这个布局与线性布局区别在orientation属性,相对布局是没有的
2.属性演示
本部分主要分两个模块
第一个使用layout_alignParentBottom 属性,字面意思是和父组件底部一条线,也就是并齐,以此类推,layout_alignParentRight意思是和父组件右部一条线
第二个介绍两个组件之间的相对关系,分别定义两个view组件,并利用id属性分别给两个组件定义了唯一标识,layout_toRightOf属性表示在谁的右边,后面填写了刚刚定义的view1的id,layout_below="@id/v2_1"可以看出定义在谁的下面。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#CC3352"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
></View>
<View
android:id="@+id/v2_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#99FF00"></View>
<View
android:id="@+id/v2_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FF0033"
android:layout_toRightOf="@id/v2_1"
android:layout_below="@id/v2_1"
></View>
</RelativeLayout>
利用已经学习的线性布局和相对布局综合一下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#00FFFF"
android:layout_marginLeft="50dp">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#CC3352"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
></View>
<View
android:id="@+id/v2_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#99FF00"></View>
<View
android:id="@+id/v2_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FF0033"
android:layout_toRightOf="@id/v2_1"
android:layout_below="@id/v2_1"
></View></RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#000000"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<View
android:id="@+id/v3_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#9F79EE"></View>
<View
android:id="@+id/v3_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#9AFF9A"
android:layout_marginLeft="10dp" ></View>
<View
android:id="@+id/v3_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#9F79EE"
android:layout_marginLeft="10dp"></View>
</LinearLayout>
</LinearLayout>