【笔记】Android布局

前言

Android布局学习笔记

LinearLayout

android:orientation="":布局方向,不指定方向默认为横向

horizontal:横向布局
vertical:纵向布局

android:gravity="":该组件包含的子元素的对齐方式,可以使用多种方式组合,使用|间隔

center:完全居中
center_horizontal:水平居中
center_vertical:垂直居中
top:顶部
bottom:底部
left:居左
right:居右

android:layout_gravity="":该组件在父元素的对齐方式,可以使用多种方式组合,使用|间隔

center:完全居中
center_horizontal:水平居中
center_vertical:垂直居中
top:顶部
bottom:底部
left:居左
right:居右

android:background="":背景颜色
android:divider="":分割线图片,需要与showDividers组合使用
android:showDividers="":显示分割线的位置

none:不显示分割线
beginning:在布局开始位置显示
end:在布局结束位置显示
middle:在两个布局中间位置显示

android:dividerPadding="":分割线左右内间距
android:layout_weight="":布局权重,多个布局同时使用权重属性时,会根据各个布局的权重作为比值,划分布局在屏幕中的尺寸。权重会在原有尺寸基础上增加布局的空间,分配的是剩余空间,所以为了可以完全通过权重控制布局尺寸,应当将所有布局的对应尺寸设置为0dp(如果使用横向布局,就将设置为0dp;如果使用纵向布局,就将设置为0dp)

1
2
3
4
5
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>

RelativeLayout

  • 默认相对于父容器的左上角对齐

相对于父容器定位

android:layout_alignParentLeft="true":相对于父容器左对齐
android:layout_alignParentRight="true":相对于父容器右对齐
android:layout_alignParentTop="true":相对于父容器顶部对齐
android:layout_alignParentBottom="true":相对于父容器底部对齐
android:layout_centerHorizontal="true":相对于父容器水平居中
android:layout_centerVertical="true":相对于父容器垂直居中
android:layout_centerInParent="true":相对于父容器完全居中

1
2
3
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />

相对于兄弟容器定位

android:layout_above="@id/编号":放置于兄弟容器上边
android:layout_below="@id/编号":放置于兄弟容器下边
android:layout_toLeftOf="@id/编号":放置于兄弟容器左边
android:layout_toRightOf="@id/编号":放置于兄弟容器右边
android:layout_alignTop="@id/编号":对齐于兄弟容器上边界
android:layout_alignBottom="@id/编号":对齐于兄弟容器下边界
android:layout_alignLeft="@id/编号":对齐于兄弟容器左边界
android:layout_alignRight="@id/编号":对齐于兄弟容器右边界

1
2
3
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />

边距

android:layout_margin="":四周外边距
android:layout_marginTop="":上外边距
android:layout_marginBottom="":下外边距
android:layout_marginLeft="":左外边距
android:layout_marginRight="":右外边距
android:padding="":四周内边距
android:paddingTop="":上内边距
android:paddingBottom="":下内边距
android:paddingLeft="":左内边距
android:paddingRight="":右内边距

1
2
3
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />

FrameLayout

android:foreground="@drawable/ic_图片名":前景图
android:foregroundGravity="":前景图位置

top:顶部
bottom:底部
left:左侧
right:右侧

1
2
3
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent" />

TableLayout

父布局属性

android:collapseColumns="":设置需要被隐藏的列的编号,编号从0开始,多个编号用0隔开
android:stretchColumns="":设置允许被拉伸的列的编号,编号从0开始,多个编号用0隔开。拉伸时要有剩余空间才会生效
android:shrinkColumns="":设置允许被收缩的列的编号,编号从0开始,多个编号用0隔开。收缩时要有超出的部分才会生效

子布局属性

android:layout_column="":设置从指定编号位置开始显示
android:layout_span="":设置从指定编号位置开始显示

1
2
3
4
5
6
7
8
9
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TableRow>
...
</TableRow>

</TableLayout>

GridLayout

父布局属性

android:orientation="":显示方式

horizontal:横向显示
vertical:纵向显示

android:columnCount="":每行最多显示的列数

子布局属性

android:layout_row="":显示在指定行,行号从0开始
android:layout_column="":显示在指定列,列号从0开始
android:layout_rowWeight="":行权值,通常与多个子元素组合使用,实际行高为多个子元素权值的比值
android:layout_columnWeight="":列权值,通常与多个子元素组合使用,实际列宽为多个子元素权值的比值

1
2
3
4
5
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

</GridLayout>

ConstraintLayout

1
2
3
4
5
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>

添加和设置组件

添加引导线

添加约束

自动生成约束

清除所有约束

完成

参考文献

哔哩哔哩——Android架构解析