1.线性布局
线性布局(LinearLayout),将其中的控件在线性方向上依次排列,通过android:orientation属性指定其控件的排列方向,默认横向排列
属性 | 描述 |
---|---|
android:orientation=“vertical” | 纵向排列,每行仅包含一个界面元素 |
android:orientation=“horizontal” | 横向排列,每列仅包含一个界面元素 |
基本属性:
属性 | 描述 |
---|---|
android:layout_margin=“10dp” | 外边距 |
android:layout_padding=“10dp” | 内边距 |
2.相对布局
相对布局(RelativeLayout),利用控件之间的相对位置关系来进行布局,控件与父容器、控件与其他控件之间的相对关系
true属性位置控制:
属性 | 描述 |
---|---|
android:layout_centerHorizontal | 如果为true,将该控件的置于水平居中 |
android:layout_centerVertical | 如果为true,将该控件的置于垂直居中 |
android:layout_centerInParent | 如果为true,将该控件的置于父控件的中央 |
android:layout_alignParentTop | 如果为true,将该控件的顶部与其父控件的顶部对齐 |
android:layout_alignParentBottom | 如果为true,将该控件的底部与其父控件的底部对齐 |
android:layout_alignParentLeft | 如果为true,将该控件的左部与其父控件的左部对齐 |
android:layout_alignParentRight | 如果为true,将该控件的右部与其父控件的右部对齐 |
给定ID位置控制:
属性 | 描述 |
---|---|
android:layout_below=“@id/view_1” | 将该控件置于"@id/view_1"控件下边 |
android:layout_above=“@id/view_1” | 将该控件置于"@id/view_1"控件上边 |
android:layout_toLeftOf=“@id/view_1” | 将该控件置于"@id/view_1"控件的左边 |
android:layout_toRightOf =“@id/view_1” | 将该控件置于"@id/view_1"控件的右边 |
android:layout_alignTop =“@id/view_1” | 将该控件的顶部边缘与"@id/view_1"顶部边缘对齐 |
android:layout_alignBottom =“@id/view_1” | 将该控件的底部边缘与"@id/view_1"底部边缘对齐 |
android:layout_alignLeft =“@id/view_1” | 将该控件的左边缘与给定ID的左边缘对齐 |
android:layout_alignRight =“@id/view_1” | 将该控件的右边缘与给定ID的右边缘对齐 |
3.帧布局
帧布局(FrameLayout),这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式,所以它应用的场景并不多;帧布局的大小由控件中最大的子控件决定,如果控件的大小一样大的话,那么同一时刻就只能看到最上面的那个组件!后续添加的控件会覆盖前一个,虽然默认会将控件放置在左上角,但是我们也可以通过layout_gravity属性,指定到其他的位置!
帧布局的属性只有两个特有的:
属性 | 描述 |
---|---|
android:foreground | 设置改帧布局容器的前景图像 |
android:foregroundGravity | 设置前景图像显示的位置 |
前景图像:永远处于帧布局最上面,直接面对用户的图像,就是不会被覆盖的图片。
4.绝对布局
绝对布局(AbsoluteLayout),通过指定界面元素的坐标位置,来确定用户界面的整体布局。
通过X轴和Y轴确定界面元素位置后,Android系统不能够根据不同屏幕对界面元素的位置进行调整了。
属性 | 描述 |
---|---|
android:layout_x | 控制组件X坐标 |
android:layout_y | 控制组件y坐标 |
5.表格布局
表格布局(TableLayout),适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。
全局属性:
属性 | 描述 |
---|---|
android:stretchColumns | 设置可伸展的列,该列可以向行方向伸展,最多可占据一整行 |
android:shrinkColumns | 设置可收缩的列 |
android:collapseColumns | 设置要隐藏的列 |
单元格属性:
属性 | 描述 |
---|---|
android:layout_column | 指定该单元格在第几列显示 |
android:layout_span | 指定该单元格占据的列数(未指定时,为1) |
6.网格布局
网格布局(GridLayout),类似于表格布局,是Android 4.0及以上版本新增加的布局,使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列。
属性 | 描述 |
---|---|
android:rowCount=“4” | 设置网格布局有4行 |
android:columnCount=“4” | 设置网格布局有4列 |
android:layout_row = “0” | 设置组件位于第1行 |
android:layout_column = “2” | 设置该组件位于第3列 |
android:layout_rowSpan = “2” | 纵向合并2行 |
android:layout_columnSpan = “3” | 横向横跨2列 |
7.约束性布局
约束性布局(ConstraintLayout),是Google在2016年的I/O大会上推出来的一个新的布局,根据布局中其他元素或视图,确定View在屏幕中的位置,受到其他视图、父容器和基准线三类约束。现在用的也越来越多了!!
相对定位:
属性 | 描述 |
---|---|
app:layout_constraintLeft_toLeftOf=“@id/view_1” | 将该控件的左边约束到"@id/view_1"的左边 |
app:layout_constraintLeft_toRightOf | 将该控件的左边约束到"@id/view_1"的右边 |
app:layout_constraintRight_toLeftOf | 将该控件的右边约束到"@id/view_1"的左边 |
app:layout_constraintRight_toRightOf | 将该控件的右边约束到"@id/view_1"的右边 |
app:layout_constraintTop_toTopOf | 将该控件的上边约束到"@id/view_1"的上边 |
app:layout_constraintTop_toBottomOf | 将该控件的上边约束到"@id/view_1"的下边 |
app:layout_constraintBottom_toTopOf | 将该控件的下边约束到"@id/view_1"的上边 |
app:layout_constraintBottom_toBottomOf | 将该控件的下边约束到"@id/view_1"的下边 |
app:layout_constraintBaseline_toBaselineOf | 将该控件的与"@id/view_1"对齐(大小不一致时用) |
app:layout_constraintStart_toEndOf | 将该控件开始位置约束到"@id/view_1"结束位置 |
app:layout_constraintStart_toStartOf | 将该控件开始位置约束到"@id/view_1"开始位置 |
app:layout_constraintEnd_toStartOf | 将该控件结束位置约束到"@id/view_1"开始位置 |
app:layout_constraintEnd_toEndOf | 将该控件结束位置约束到"@id/view_1"结束位置 |
角度定位:
app:layout_constraintCircle="@+id/tv_1"
app:layout_constraintCircleAngle="135"(角度)
app:layout_constraintCircleRadius="100dp"(距离)
就是tv_2的中心在tv_1的中心的135度,距离为100dp
尺寸约束:
当控件的高度或宽度为wrap_content时,可以使用以下属性来控制最大、最小的高度或宽度
属性 | 描述 |
---|---|
android:minWidth | 最小的宽度 |
android:minHeight | 最小的高度 |
android:maxWidth | 最大的宽度 |
android:maxHeight | 最大的高度 |