布局管理器
1.RelationLayout相对布局管理器
1.1 属性
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:gravity="center"
android:IgnoreGravity="@id/user"
1.2 内部类
LayoutParams
1.2.1属性
2.LinearLayout 线性布局管理器
应用
2.1属性
android:orientation="vertical" //组件排列方式
android:gravity="center_horizontal|bottom"
android:layout_weight="1" // 剩余空间分配占比,用weight标识,不写为0
3.FrameLayout 帧布局管理器
显示层叠内容,或者可拖动内容
3.1属性
android:foreground="@mipmap/ic_launcher" //前景,最前面显示的图片,不会被覆盖
android:foregroundGravity="right|bottom" //前景图片位置设置
4.TableLayout表格布局管理器
4.1属性
android:collapseColumns="0,2" // 需要隐藏的列
android:stretchColumns="1" // 可以被拉伸的列
android:shrinkColumns="2" // 可以被收缩的列
<TableRow>...</TableRow> // 一个行内含有的东西,不写就直接用组件添加一行
5.GridLayout网格布局管理器
5.1属性
android:columnCount="4" // 最大列数
android:orientation="vertical" // 默认排序方向
android:rowCount="3" // 最大行数
android:layout_column="0" // 位于第几列
android:layout_row="0" // 第几行
android:layout_columnSpan="2" // 占几列
android:layout_rowSpan="2" // 占几行
android:layout_columnWeight="1" // 列上权重
android:layout_rowWeight="1" // 行上权重
android:layout_gravity="fill" // 填充
在Java文件中,
gridLayout.getChildCount() // 子UI数量
gridLayout.getChildAt(i) // 返回子UI的View,按index来,0 ~
例如:
Random r = new Random();
GridLayout gridLayout = findViewById(R.id.gl);
for (int i = 0; i < gridLayout.getChildCount(); i ++){
TextView textView = (TextView) gridLayout.getChildAt(i);
textView.setBackgroundColor(Color.rgb(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
}
6.ConstraintLayout
https://www.bilibili.com/video/BV1w4411t7UQ?p=4
https://www.jianshu.com/p/17ec9bd6ca8a
7.布局管理器的嵌套