Android布局详解

介绍

Android中的布局分别有:LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局) FrameLayout(帧布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局)、ConstraintLayout(约束布局)等。

LinearLayout

LinearLayout(线性布局),该布局应该是 Android 视图设计中最经常使用的布局。该布局可以使放入其中的组件以水平方式或者垂直方式整齐排列,通过 android:orientation 属性指定具体的排列方式,通过 weight 属性设置每个组件在布局中所占的比重。

RelativeLayout

RelativeLayout(相对布局)是Android的六大布局之一,顾名思义就是按照组件之间的相对位置来进行布局。

TableLayout

TableLayout(表格布局) 以行列的方式也就是表格来管理组件。

<TableLayout>
	<TableRow>
	<!--子控件-->
	</TableRow>
</TableLayout >

FrameLayout

FrameLayout(单帧布局),是 Android 所提供的布局方式里最简单的布局方式
这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式

AbsoluteLayout

AbsoluteLayout(绝对布局),放入该布局的组件需要通过 android:layout_x 和 android:layout_y 两个属性指定其准确的坐标值,并显示在屏幕上。

GridLayout

GridLayout(网格布局),所切割出来的版面就如同表格一般整齐,加入的组件会按顺序由左至右、由上至下摆放,所以无法直接指定要摆放的区域。

ConstraintLayout(约束布局)

ConstraintLayout (约束布局)是一个ViewGroup,允许您灵活地定位和调整小部件大小的布局,有多种约束,相对定位、角度定位、边距、居中和偏移、尺寸和约束、链等。

优点

RelativeLayout

  1. 灵活性高
  2. 减少布局的结构层次
  3. 属性多

属性

组件与布局共有属性

  1. gravity:设置容器内组件的对齐方式,居中(center)、靠右(right、end)、垂直居中(center_vertical)、靠下(bottom)、靠左(left、start)、横向居中(center_horizontal)等
  2. Layout_gravity:设置自身相对于父组件
  3. ignoreGravity:设置了该属性为true的属性控件,将不受gravity属性的影响
  4. orientation:设置容器内组件的方向,垂直(vertical)、水平(horizontal)。
  5. divider:分割线,分割图片的id
  6. showDividers:与divider一起,无(none)、开始(beginning)、结束(end)、每两个组件间(middle)
  7. dividerPadding:分割线的Padding(dp)

ConstraintLayout

自身属性

相对定位

  1. layout_constraintLeft_toLeftOf(layout_constraintStart_toStartOf):当前组件的左侧与另一个组件的左侧位置对齐(id),与RelativeLayout的alignLeft属性相似
  2. layout_constraintLeft_toRightOf(layout_constraintStart_toEndOf):当前组件的左侧与另一个组件的右侧位置对齐(id),与RelativeLayout的toRightOf属性相似
  3. layout_constraintRight_toLeftOf(layout_constraintEnd_toStartOf):当前组件的右侧与另一个组件的左侧位置对齐(id),与RelativeLayout的toLeftOf属性相似
  4. layout_constraintRight_toRightOf(layout_constraintEnd_toEndOf):当前组件的左侧与另一个组件的左侧位置对齐(id),与RelativeLayout的alignRight属性相似
  5. layout_constraintTop_toTopOf:头部对齐,与alignTop相似
  6. layout_constraintTop_toBottomOf:当前组件在另一个组件的下侧 与below相似
  7. layout_constraintBottom_toTopOf:当前组件在另一个组件的上方 与above相似
  8. layout_constraintBottom_toBottomOf:底部对齐,与alignBottom属性相似
  9. layout_constraintBaseline_toBaselineOf::文字底部对齐,与alignBaseLine属性相似

角度定位

goneMargin(控件的可见性为gone时使用的margin值)

  1. layout_goneMarginLeft(layout_goneMarginStart):同layout_MarginLeft
  2. layout_goneMarginRight(layout_goneMarginEnd):同layout_MarginRight
  3. layout_goneMarginTop:同layout_MarginTop
  4. layout_goneMarginBottom:同layout_MarginBottom

居中和偏移

居中

居中时使用margin也会进行偏移

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
偏移
  1. layout_constraintHorizontal_bias:水平偏移,范围(0-1)、0(最左侧)、1(最右侧)、0.5(居中)、0.3(左倾)
  2. layout_constraintVertical_bias :垂直偏移,范围(0-1)、0(最上侧)、1(最下侧)、0.5(居中)、0.3(上倾)

尺寸约束

1.layout_constraintDimensionRatio:设置宽高比,正方形(1:1)、高:宽=2:3(H,2:3)、宽:高=2:3(W,2:3)

  1. layout_constraintHorizontal_chainStyle:纵向链的样式,默认展开元素(CHAIN_SPREAD)、展开元素但链的两端贴近parent(CHAIN_SPREAD_INSIDE)、链的元素将被打包在一起(CHAIN_PACKED)
  2. layout_constraintHorizontal_weight:纵向权重

辅助工具

Optimizer

当我们使用 MATCH_CONSTRAINT 时,ConstraintLayout 将对控件进行 2 次测量,ConstraintLayout在1.1中可以通过设置 layout_optimizationLevel 进行优化,可设置的值有,默认仅优化直接约束和屏障约束、无优化(none)、(standard)、优化直接约束(direct)、优化屏障约束(barrier)、优化链约束(chain)、优化尺寸测量(dimensions)

Barrier

Barrier可以在多个控件的一侧建立一个屏障

  1. barrierDirection:屏障所在的位置:bottom、end、left、right、start、top
  2. constraint_referenced_ids:屏障引用的控件,可设置多个(用“,”隔开)
<androidx.constraintlayout.widget.Barrier
	android:id="@+id/barrier"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	app:barrierDirection="right"
	app:constraint_referenced_ids="tv1,tv2" />
Group

Group可以把多个控件归为一组,方便隐藏或显示一组控件

<androidx.constraintlayout.widget.Group
	android:id="@+id/group"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:visibility="invisible"
	app:constraint_referenced_ids="tv1,tv2" />
Guideline

Guildline像辅助线一样,在预览的时候帮助你完成布局(不会显示在界面上)。

  1. android:orientation:垂直vertical,水平horizontal
  2. layout_constraintGuide_begin:开始位置
  3. layout_constraintGuide_end:结束位置
  4. layout_constraintGuide_percent:距离顶部的百分比(orientation = horizontal时则为距离左边)
<androidx.constraintlayout.widget.Guideline
	android:id="@+id/guideline2"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:orientation="vertical"
	app:layout_constraintGuide_percent="0.5" />

TableLayout自身属性

  1. collapseColumns:设置需要被隐藏的列的序号例如,“0,2”、“1”、"*"(所有)。
  2. shrinkColumns:设置允许被收缩的列的列序号例如,“0,2”、“1”、"*"(所有)
  3. stretchColumns:设置运行被拉伸的列的列序号例如,“0,2”、“1”、"*"(所有)
  4. layout_column:跳格子,从1开始算起
  5. layout_span:合并单元格,这个组件占几个单元格

FrameLayout自身属性

  1. foreground:设置改帧布局容器的前景图像(图像id)
  2. foregroundGravity:设置前景图像显示的位置(位置)

GridLayout

自身属性

  1. rowCount:设置网格布局行数(2)
  2. colimnCount:设置网格布局列数(2)

GridLayout组件属性

  1. layout_row:设置组件位于第(2)行
  2. layout_column:设置组件位于第(3)行
  3. layout_rowSpan:设置组件横跨(2)列
  4. layout_columnSpan:设置组件横跨(2)行
  5. android:layout_gravity = “fill”:组件填满横越过的行或列

RelativeLayout子控件属性:

根据父容器定位:值为true或false

  1. layout_centerHrizontal:水平居中
  2. layout_centerVertical:垂直居中
  3. layout_centerInparent:相对于父控件完全居中
  4. layout_alignParentBottom:贴紧父控件的下边缘
  5. layout_alignParentLeft:贴紧父控件的左边缘
  6. layout_alignParentRight:贴紧父控件的右边缘
  7. layout_alignParentTop:贴紧父控件的上边缘
  8. layout_alignWithParentIfMissing:如果对应的兄弟控件找不到的话,就以父控件作为参照物

根据兄弟组件定位:兄弟的id

  1. layout_below:在兄弟的下方
  2. layout_above:在兄弟的上方
  3. layout_toLeftOf:在兄弟的左边
  4. layout_toRightOf:在兄弟的右边
  5. layout_alignTop:自身的上边缘与兄弟的上边缘对齐
  6. layout_alignLeft:自身的左边缘与兄弟的左边缘对齐
  7. layout_alignBottom:自身的下边缘与兄弟的下边缘对齐
  8. layout_alignRight:自身的右边缘与兄弟的右边缘对齐

通用

margin(偏移):属性值设备独立像素(dp、dip)、像素(px)、等

  1. layout_margin:外边距(上下左右)的距离
  2. layout_marginLeft:外边距(左)的距离
  3. layout_marginRight:外边距(右)的距离
  4. layout_marginTop:外边距(上)的距离
  5. layout_marginBottom:外边距(下)的距离

padding(填充):属性值设备独立像素(dp、dip)、像素(px)、等

  1. padding:内边距边距(上下左右)的距离
  2. paddingTop:内边距边距(上)的距离
  3. paddingRight:内边距边距(右)的距离
  4. paddingLeft:内边距边距(左)的距离
  5. paddingBottom:内边距边距(下)的距离

尺寸约束:最小、最大

  1. android:minWidth 最小的宽度
  2. android:minHeight 最小的高度
  3. android:maxWidth 最大的宽度
  4. android:maxHeight 最大的高度

  1. 不是兄弟组建的不能使用RelativeLayout的根据兄弟组件定位的属性
  2. margin可以设置为负数
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值