Android-初探布局管理器

本文深入解析Android中的多种布局方式,包括绝对布局、线性布局、表格布局、帧布局及约束布局的特点与应用。详细介绍了各布局的XML属性设置,如LinearLayout的权重分配、TableLayout的列操作、FrameLayout的组件堆叠以及ConstraintLayout的灵活性,帮助开发者掌握不同场景下的最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android9里,GridLayout 和 RelativeLayout 不被推荐使用,而推荐使用约束布局 ConstraintLayout ,所以就不做详细介绍。

AbsolutelyLayout
绝对布局,开发者直接指定组件的大小位置。只适和某些针对性的显示屏,不具有普适性。

LinearLayout

线性布局属于比较基础比较熟悉的布局,它可以控制各个组件 横向/纵向 排列。但是Android的线性布局不会换行,当组件排列到头后,剩余的组件将显示不出来。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    //排列方式:vertical纵向,horizontal横向
    android:orientation="vertical"
    
    android:id="@+id/main_layout"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

   <Button
    android:id="@+id/bt1"
    android:text="普通按钮1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    
    //此处省略剩下的五个button......

</LinearLayout>
手机运行效果

如图所示,按钮6并没有显示出来;
手机运行效果
在线性布局中,还可以通过权重来分配布局空间:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/main_layout"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">
	
   <Button
    android:id="@+id/bt1"
    android:text="普通按钮1"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"/>
    
    <Button
        android:id="@+id/bt2"
        android:text="普通按钮2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
        
    //此处省略剩下的四个button......

</LinearLayout>

权重:将layout_width设置为 0dp ,通过 layout_weight 设置占用空间的比例(所占空间比例 = 该组件 layout_weight / 所有组件 layout_weight 之和)

手机运行效果

TableLayout

表格布局继承了线性布局,因此本质上依旧是线性布局管理器。
表格布局通过添加 tablerow、其他组件来控制表格的行数列数。
TableRow 也是一个容器,可以向里面添加其他组件,每添加一个组件,该TableRow 就增加一行。

XML属性相关方法相关方法
android:collapseColumnssetCollapseColumns(int,boolean)设置需要被隐藏的列的序列号。多个序列号间用逗号隔开
android:shrinkColumnssetShrinkColumns(boolean)设置需要被收缩的列的序列号。多个序列号间用逗号隔开
android:stretchColumnssetStretchColumns(boolean)设置需要被拉伸的列的序列号。多个序列号间用逗号隔开

注:序列号从0开始

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/main_layout"
    tools:context=".MainActivity">

    <TableLayout
        android:id="@+id/tl1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="2">
        <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独占一行的普通按钮"/>
        <TableRow>
            <Button
                android:id="@+id/bt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
            <Button
                android:id="@+id/bt3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="收缩的按钮"/>
            <Button
                android:id="@+id/bt4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸的按钮"/>
        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/tl2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1">
        <Button
            android:id="@+id/bt5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独占一行的普通按钮"/>
        <TableRow>
            <Button
                android:id="@+id/bt6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
            <Button
                android:id="@+id/bt7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="隐藏的按钮"/>
            <Button
                android:id="@+id/bt8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/tl3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1,2">
        <Button
            android:id="@+id/bt9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独占一行的普通按钮"/>
        <TableRow>
            <Button
                android:id="@+id/bt10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
            <Button
                android:id="@+id/bt11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸的按钮"/>
            <Button
                android:id="@+id/bt12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸的按钮"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

效果如图所示:
手机运行效果

FrameLayout

帧布局会为加入的组件创建一个空白的区域(一帧),这些帧会根据gravity属性自动对齐,如果没有规定线性显示,那么最后加入的组件会显示在最上面。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/main_layout"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="350dp"
        android:height="350dp"
        android:layout_gravity="center"
        android:background="#F44336"/>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="300dp"
        android:height="300dp"
        android:layout_gravity="center"
        android:background="#E91E63"/>
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="250dp"
        android:height="250dp"
        android:layout_gravity="center"
        android:background="#9C27B0"/>
    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="200dp"
        android:height="200dp"
        android:layout_gravity="center"
        android:background="#673AB7"/>
    <TextView
        android:id="@+id/tv5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="150dp"
        android:height="150dp"
        android:layout_gravity="center"
        android:background="#3F51B5"/>
    <TextView
        android:id="@+id/tv6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="100dp"
        android:height="100dp"
        android:layout_gravity="center"
        android:background="#03A9F4"/>

</FrameLayout>

可以看见帧布局效果类似于三维的重叠:
手机运行效果

ConstraintLayout

约束布局感觉是灵活版的绝对布局,你可以设置各个控件的位置,但是不是强行指定位置和大小,因此,约束布局对于不同的屏幕都具有普适性。可以把约束布局比作会随屏幕缩放的图片。

具体内容参照:
约束布局ConstraintLayout

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值