Android客户端UI布局笔记|青训营笔记

112 阅读3分钟

这是我参与「第四届青训营」笔记创作活动的第十一天。这篇笔记主要是对安卓客户端常规&高级UI编程”这节课中UI布局相关部分的记录。


笔记

布局

LinearLayout(线性布局)

  • 以水平/垂直方向排列组件
  • 所有子视图在单个方向(水平或垂直)保持对齐
  • 适用于线性排列场景
属性功能描述
android:orientation布局内组件的排列方向
android:layout_weight布局内组件大小权重
android:divider布局内组件分割线
android:showDividers布局内组件间分割线位置
android:dividerPadding布局内分割线padding

RelativeLayout(相对布局)

  • 通过相对父容器/兄弟组件位置排列组件
  • 可以消除嵌套视图并使布局层次结构保持扁平化
  • 一个RelativeLayout就可以替换多个嵌套的LinerLayout组
  • 适用于复杂场景

根据父容器定位

属性功能描述
android:layout_centerInParent组件位于父容器中央位置
android:layout_centerHorizontal组件位于父容器水平中央位置
android:layout_centerVertical组件位于父容器垂直中央位置
android:layout_alignParentTop组件与父容器顶部对齐
android:layout_alignParentLeft组件与父容器左部对齐
android:layout_alignParentRight组件与父容器右部对齐
android:layout_alignParentBottom组件与父容器底部对齐

根据兄弟组件定位

属性功能描述
android:layout_above组件位于某组件上部
android:layout_below组件位于某组件下部
android:layout_toLeftOf组件位于某组件左部
android:layout_toRightOf组件位于某组件右部
android:layout_alignTop组件与某组件顶部对齐
android:layout_alignLeft组件与某组件左部对齐
android:layout_alignRight组件与某组件右部对齐
android:layout_alignBottom组件与某组件底部对齐

FrameLayout(层级布局)

  • 以层级叠加的方式排列组件
  • 适用于层级排列场景
属性功能描述
android:foreground设置前景图像
android:foregroundGravity设置前景图像Gravity
  • 前景图像:永远处于FrameLayout最上层,不会被覆盖的图片

ConstraintLayout

  • 通过约束组件位置排列组件
  • 扩展布局方式
  • 所有的视图均根据同级视图与父布局之间的关系进行布局
  • 可使用扁平视图层次层次结构(无嵌套视图组)创建复杂的大型布局
  • 为获得更好的性能与工具支持,推荐使用ConstraintLayout
  • 适用于复杂场景
属性功能描述
layout_constraintLeft_toLeftOf组件左部与某组件左部对齐
layout_constraintLeft_toRightOf组件左部与某组件右部对齐
layout_constraintRight_toLeftOf组件右部与某组件左部对齐
layout_constraintRight_toRightOf组件右部与某组件右部对齐
layout_constraintTop_toTopOf组件顶部与某组件顶部对齐
layout_constraintTop_toBottomOf组件顶部与某组件底部对齐
layout_constraintBottom_toTopOf组件底部与某组件顶部对齐
layout_constraintBottom_toBottomOf组件底部与某组件底部对齐
layout_constraintBaseline_toBaselineOf组件基线与某组件基线对齐
layout_constraintStart_toEndOf组件首部与某组件尾部对齐
layout_constraintStart_toStartOf组件首部与某组件首部对齐
layout_constraintEnd_toStartOf组件尾部与某组件首部对齐
layout_constraintEnd_toEndOf组件尾部与某组件尾部对齐

总结

通过本节课的学习,我学习到了Android的布局类型及其相关用法,知道了平常使用的安卓app的界面是怎样构造的,了解颇多。