Android 布局
线性布局LinearLayout
属性
- android:orientation:设置布局方向
-
vertical:垂直方向
- horizontal:水平方向
- horizontal:水平方向
-
- android:gravity:设置子元素对齐方式
top、bottom、start、end、left、right
支持以|分隔的多个组合- android:layout_gravity:在子控件使用,设置子控件在父控件的位置
- android:weightSum: 在布局方向上将剩余空间分成几份(单独配置无效)
- android:layout_weight: 设置子空间占用几份
可以看到父控件将剩余空间分成3份,textView占用空间是button的2倍
注意:weightSum是可以不设置的。如果weightSum没有设置,Android会根据子组件所设置的layout_weight计算出weightSum的值。
- android:layout_weight: 设置子空间占用几份
- android:divider:为LinearLayout设置分割线所使用的的图片 需要配置android:showDividers使用
- android:showDividers:设置分割线所在位置,值为:none, middle, begining, end
- android:dividerPadding:设置分割线的Padding。
相对布局RelativeLayout
属性
- android:gravity: 控制该组件所包含的子元素的对齐方式,可以以|分隔组合使用
- android:ignoreGravity:设置该组件是否受gravity影响,ture不受影响,反之受影响
- 根据父组件定位
- android:layout_alignParentLeft:左对齐
- android:layout_alignParentRight:右对齐
- android:layout_alignParentTop:顶部对齐
- android:layout_alignParentBottom:底对齐
- android:layout_centerHorizontal:水平居中
- android:layout_centerVertical:垂直居中
- android:layout_centerInParent:根据父容器位置居中
- 根据同级组件对齐 (值是同级组件id)
- android:layout_toLeftOf:在同级组件左侧
- android:layout_toRightOf:在同级组件右侧
- android:layout_above:在同级组件上面
- android:layout_below:在同级组件下面
- android:layout_alignTop:在同级组件的上边界
- android:layout_alignBottom:在同级组件的下边界
- android:layout_alignLeft:在同级组件的左边界
- android:layout_alignRight:在同级组件的右边界
表格布局TableLayout
属性
- android:collapseColumns:需要被隐藏的列序号,从0开始
- android:shrinkColumns:允许收缩的列序号,从0开始
- android:layout_column:跳过几个,从0开始
- android:layout_span:合并几个单元格
帧布局FrameLayout
属性
- android:foreground:设置改帧布局容器的前景图像
- android:foregroundGravity:设置前景图像显示的位置
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:foreground="@drawable/logo"
android:foregroundGravity="right|bottom">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FF6143" />
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#7BFE00" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFFF00" />
</FrameLayout>
网格布局GridLayout
属性
- android:orientation: 设置布局方向
- horizontal:水平方向
- vertical:竖直方向
- android:layout_gravity: 设置子控件在父控件的位置
- android:rowCount:网格布局行数
- android:columnCount:网格布局列数
- android:layout_row:设置组件在哪一行
- android:layout_column:设置组件在哪一列
- android:rowSpan:设置组件合并哪几行
- android:columnSpan:设置组件合并那几列
约束布局ConstraintLayout
属性
- 方向约束
- app:layout_constraintTop_toTopOf=“” 我的顶部和谁的顶部对齐
- app:layout_constraintBottom_toBottomOf=“” 我的底部和谁的底部对齐
- app:layout_constraintLeft_toLeftOf=“” 我的左边和谁的左边对齐
- app:layout_constraintRight_toRightOf=“” 我的右边和谁的右边对齐
- app:layout_constraintStart_toStartOf=“” 我的开始位置和谁的开始位置对齐
- app:layout_constraintEnd_toEndOf=“” 我的结束位置和谁的结束位置对齐
- app:layout_constraintTop_toBottomOf=“” 我的顶部位置在谁的底部位置
- app:layout_constraintStart_toEndOf=“” 我的开始位置在谁的结束为止
- …以此类推
- 基线对齐
app:layout_constraintBaseline_toBaselineOf=“” - 角度约束
- app:layout_constraintCircle=“” 目标控件id
- app:layout_constraintCircleAngle=“” 对于目标的角度(0-360)
- app:layout_constraintCircleRadius=“” 到目标中心的距离
- 百分比偏移
- app:layout_constraintHorizontal_bias=“” 水平偏移 取值范围是0-1的小数
- app:layout_constraintVertical_bias=“” 垂直偏移 取值范围是0-1的小数