一、线性布局 LinearLayout
常用属性:
android:id
android:layout_width=“match_parent” 设置宽度,匹配父控件
android:layout_height =“wrap_content” 设置高度,表示当前元素高度正好包含里面的内容
android:layout_width=“200dpt” 设置固定宽度
android:background="#000000" 设置背景颜色为黑色
android:layout_margin
android:layout_padding="20dp"设置内部距外边框距离20单位
android:layout_paddingLeft="20dp"设置内部距左侧外边框距离20单位
android:layout_paddingRight="20dp"设置内部距右侧外边框距离20单位
android:layout_paddingTop="10dp"设置内部距上侧外边框距离10单位
android:layout_paddingBottom="30dp"设置内部距下侧外边框距离30单位
android:orientation=“vertical” 设置为垂直方向排列的信息
android:orientation=“horizontal” 设置为水平方向排列的信息
android:gravity=“center_vertical” 设置垂直方向的居中
android:gravity="center_horizontal"设置水平方向的居中
android:gravity="center"设置居中
平分区域
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:background="#0066FF"
android:orientation="horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FF0033"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#55AA99"
android:layout_weight="1"/>
</LinearLayout>
二、相对布局
常用属性
android:layout_toLeftOf 设置在谁的左边
android:layout_toRightOf 设置在谁的右边
android:layout_ below 设置在谁的下边
android:layout_ above 设置在谁的上方
android:layout_alignBottom 设置在谁的底部对齐
android:layout_alignTop 设置在谁的顶部对齐
android:layout_alignBaseline 设置在谁的水平对齐
android:layout_ alignLeft 设置跟谁的左边对齐
android:layout_ alignRight 设置跟谁的右边对齐
android:layout_alignParentLeft 其属性值为boolean值,用于指定该组件是否与布局管理器左边对齐
android:layout_alignParentRight 其属性值为boolean值,用于指定该组件是否与布局管理器右边对齐
android:layout_alignParentBottom 其属性值为boolean值,用于指定该组件是否与布局管理器底端对齐
android:layout_alignParentTop 其属性值为boolean值,用于指定该组件是否与布局管理器顶端对齐
android:layout_centerVertical 在父元素中垂直居中
android:layout_centerHorizontal 在父元素中水平居中
android:layout_centerInParent 在父元素中正中间
android:layout_centerParent 在父元素中居中
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000"
/>
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FF0033"
android:layout_below="@id/view_1"
/>
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/view_2"
android:orientation="horizontal"
android:background="#0066FF"
android:padding="15dp"
>
<View
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF0033"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:padding="15dp">
<View
android:id="@+id/view_3"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF9900"/>
<View
android:id="@+id/view_4"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF9900"
android:layout_toRightOf="@id/view_3"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
效果如图
三、TableLayout表格布局
1、表格布局与常见的表格类似,以行列的方式来管理放入其中的组件。
2、在其中通过添加标记来表示行,也是容器,可向该标记中添加其他组件,每添加一个组件,表格就会增加一列。
3、表格布局的语法如下:
<TableLayout
属性列表...
>
<TableRow
组件列表...
>
</TableRow>
</TableLayout>
TableLayout布局常见的XML属性列表的使用
android:collapseColumns
设置需要隐藏的列的序列号(序列号从0开始),多个用列序号用逗号隔开
android:shrinkColumns
设置允许被收缩的列的序列号,多个用列序号用逗号隔开。
android:stretchColimns
设置允许被拉伸的列的序列号,多个用列序号用逗号隔开。
四、帧布局
android:layout_gravity=“left” 左对齐
android:layout_gravity=“bottom|right” 右和底部对齐
android:layout_gravity=“center” 中间对齐