Android基础之--6大常用布局的使用总结

总结一下常用的6种布局,对应的位置属性

布局通用属性功能
android:id设置布局的标识
android:layout_width设置布局的宽度
android:layout_height设置布局的高度
android:layout_margin设置布局外边距
android:gravity设置控件位于布局中的相对位置
android:padding设置布局内边距
android:background设置布局的背景
android:foreground设置帧布局容器的前景图像
android:foregroundGravity设置前景图像显示的位置
android:foregroundTint设置前景图像显示的色调

边框stroke无法在xml中设置,需要①通过定义Drawable资源;②使用View的setBorder方法来设置布局的边框

布局继承自ViewGroup

1. LinearLayout线性布局

线性布局,以水平(horizontal)或垂直(vertical)方向排列布局中的所有控件,其中控件的排列方向是必写项,不写的话布局中的控件有可能会显示不全。

LinearLayout属性功能
android:orientation设置布局内控件的排列顺序(默认水平)
android:weight在布局内设置控件权重

2. RelativeLayout相对布局

相对布局以父容器为参照物,指定子控件在父容器中的相对位置。

RelativeLayout中控件属性功能
android:layout_centerInParent当前控件位于父布局的中央位置
android:layout_centerVertical当前控件位于父布局的垂直居中位置
android:layout_centerHorizontal当前控件位于父控件的水平居中位置
android:layout_above当前控件位于某控件上方
android:layout_below当前控件位于某控件下方
android:layout_toLeftOf当前控件位于某控件左侧
android:layout_toRightOf当前控件位于某控件右侧
android:layout_alignParentTop当前控件与父控件顶端对齐
android:layout_alignParentLeft当前控件与父控件左对齐
android:layout_alignParentRight当前控件与父控件右对齐
android:layout_alignParentBottom当前控件与父控件底端对齐
android:layout_alignTop当前控件与某控件上边界对齐
android:layout_alignBottom当前控件与某控件下边界对齐
android:layout_alignLeft当前控件与某控件左边界对齐
android:layout_alignRight当前控件与某控件右边界对齐

3. FrameLayout帧布局

帧布局直接在屏幕上开辟出一块空白的区域,没有任何的定位方式,布局中的所有控件会根据添加顺序先后叠放在布局的左上角,可以使用android:gravity或者android:layout_gravity属性来修改控件位置,但记住,后添加的始终会叠放在先添加的上层。

4. ConstraintLayout约束布局

约束布局是最灵活、最好玩的一种布局啦!约束布局的出现是为了解决开发过程中过于复杂的页面层级嵌套过多的问题(层级过深会增加绘制界面需要的时间,影响用户体验),能够以灵活的方式定位和调整控件位置。

使用ConstraintLayout布局时至少要保证水平和垂直方向都至少有一个约束才能确定控件的位置。
布局特点:

  • 相对定位:可以通过约束条件将视图相对于父容器或其他视图进行定位,而不需要使用嵌套布局;
  • 弹性尺寸:可以通过设置约束条件来调整视图的大小和比例,以适应不同屏幕尺寸和方向的变化;
  • 辅助线:可以使用辅助线(Guideline)来辅助布局,例如将视图与屏幕的边缘或其他视图的对齐;
  • 链式布局:可以使用链式约束来创建视图链,比如水平或垂直的线性链。

(1) 基本方向约束

    // 当前控件的某条边 与 某控件某条边 对齐
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="@+id/btn"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/btn"

(2)百分比偏移Bias

    // 当前控件位于父布局水平或垂直方向某百分比位置
    // 使用的前提是 设置该属性的方向上始末位置都有约束
    app:layout_constraintHorizontal_bias="0.8"
    app:layout_constraintVertical_bias="0.2"

(3)基线对齐Baseline

    // 当前控件某边的基线与某控件的某边基线对齐,如上标或下标的实现
    app:layout_constraintBaseline_toBaselineOf="@id/text_view"

(4)角度约束Circle

    // 当前控件位于某控件某角度多远位置
    app:layout_constraintCircle="@+id/image_view"     // 目标控件id
    app:layout_constraintCircleAngle="45"    // 对于目标的角度(0-360)
    app:layout_constraintCircleRadius="80dp"    // 到目标中心的距离

(5)控件宽高比Ratio

    // 前提:宽高有一个需要设置为0dp
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintDimension="0.5"

(6)辅助线Guideline

    // 辅助线控件
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3" />

    // 约束方式:百分比、始末位置
    app:layout_constraintGuide_percent="0.3"
    app:layout_constraintGuide_begin="50dp"
    app:layout_constraintGuide_end="100dp"

(7)链式Chains

    // 将许多个控件在水平或者垂直方向,形成一条链,用于平衡这些控件的位置
    // 前提是在该方向的所有控件首位互相约束,形成一条链,链式属性在第一个控件内设置
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/text2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside"
        android:textSize="30sp"
        android:text="哈哈哈哈哈"
        />

    // 约束方式
    spread : 均分剩余空间
    spread_inside:两侧的控件贴近两边,剩余的控件均分剩余空间
    packed:所有控件贴紧居中

    // Chains支持weight的配置(对应方向的尺寸改为0dp)
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintVertical_weight="3"

(8)其他

    // 屏障Barrier:防止控件长度变化与其他控件重叠显示
    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="textView2,textView1" />

    // 组Group:控制多控件的显隐状态
    <androidx.constraintlayout.widget.Group
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:constraint_referenced_ids="A,B,C" />

5. TableLayout表格布局

表格布局(继承自LinearLayout)是采用行、列的形式来管理布局控件。
不需要声明包含多少行、多少列,面是通过在TableLayout布局中添加TableRow布局或控件来控制表格的行数,可以在TableRow 布局中添加控件来控制表格的列数。

TableLayout属性功能
android:stretchColumns设置可被拉伸的列,填满剩下的多余空白空间
android:shrinkColumns设置可被收缩的列,当可收缩的列太宽(内容过多)不会被挤出屏幕,如文字会换行
android:collapseColumns设置可被隐藏的列
TableLayout中控件属性功能
android:layout_column设置该控件显示位置
android:layout_span设置该控件占几行,默认是1行
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="500dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/purple_200"
        android:collapseColumns="0">
        
        <TableRow>
            // 位于第0列的按钮1被隐藏,只显示按钮2
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:text="按钮1"
                android:layout_span="0"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="按钮2" />
        </TableRow>
    </TableLayout>

6. GridLayout网格布局

网格布局用于将多控件整齐排列起来,个别位置需要个性化处理的话可以参考《Android基础之–GridLayout合并网格》

GridLayout属性功能
layout_rowCount网格布局最大行数
layout_columnCount网格布局最大列数
GridLayout中控件属性功能
layout_row显示在第几行
layout_column显示在第几列
layout_rowSpan纵向跨几行
layout_columnSpan横向跨几列
layout_rowWeight纵向剩余空间分配方式
layout_columnWeight横向剩余空间的分配方式

最后

如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。

如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
img
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。

欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓(文末还有ChatGPT机器人小福利哦,大家千万不要错过)

PS:群里还设有ChatGPT机器人,可以解答大家在工作上或者是技术上的问题
图片

  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值