百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Android常用布局总结之(LinearLayout、GridLayout等4种)

zhezhongyun 2025-05-28 21:41 63 浏览

一、LinearLayout 线性布局

LinearLayout 是一个视图组,用于使所有子视图在单个方向(垂直或水平)保持对齐。您可以使用 android:orientation 属性指定布局方向。

  • android:orientation,指定布局方向,vertical-竖向布局,horizontal-横向布局
  • android:layout_weight,为各个子视图分配权重(数字)
  • android:layout_gravity,容器自身相对于的上级容器的排列方式
  • android:gravity,容器内部组件的排列方式(top,bottom,start,end,center)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="文本标签1"
        android:background="@color/teal_700"
        android:textColor="@color/white"
        android:textSize="60sp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="文本标签2"
        android:background="@color/teal_200"
        android:textColor="@color/white"
        android:textSize="60sp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="文本标签3"
        android:background="@color/purple_200"
        android:textColor="@color/white"
        android:textSize="60sp"
        />
</LinearLayout>

线性布局 - 均等分布

如果要让每个子视图使用大小相同的屏幕空间,请将每个视图的 android:layoutheight 设置为 "0dp"(针对垂直布局),或将每个视图的 android:layoutwidth 设置为 "0dp"(针对水平布局)。然后,将每个视图的 android:layout_weight 设置为 "1"。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="文本标签1"
        android:background="@color/teal_700"
        android:textColor="@color/white"
        android:textSize="60sp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="文本标签2"
        android:background="@color/teal_200"
        android:textColor="@color/white"
        android:textSize="60sp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="文本标签3"
        android:background="@color/purple_200"
        android:textColor="@color/white"
        android:textSize="60sp"
        />
</LinearLayout>

二、TableLayout 表格布局

1、TableLayout继承了LinearLayout

  • android:stretchColumns,设置需要被隐藏的列的序号,从0开始,多个用,隔开
  • android:collapseColumns,设置需要被隐藏的列的序号,从0开始,多个用,隔开
  • android:shrinkColumns,设置允许被收缩的列的列序号,从0开始,多个用,隔开

2、子控件设置属性

  • android:layout_span,横向跨几列
  • android:layout_column,显示在第几列,从0开始

3、子控件TableRow,控制每行显示数据

4、如何确定行数与列数

  1. 如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!
  2. 如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!
  3. tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定
  4. tablerow的layoutwidth属性,默认是fillparent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrapten——content的,我们却可以自己设置大小!
  5. 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
  6. 有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns=""
    android:stretchColumns="1"
    android:shrinkColumns="2"
    >
    <TableRow>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本1"
        android:background="@color/teal_700"
        android:textColor="@color/white"
        android:textSize="40sp"
        android:layout_span="1"
        android:layout_column="0"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本2"
        android:background="@color/teal_200"
        android:textColor="@color/white"
        android:textSize="40sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本3文本3文本3文本3"
        android:background="@color/purple_200"
        android:textColor="@color/white"
        android:textSize="40sp"
        />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文本4"
            android:background="@color/teal_700"
            android:textColor="@color/white"
            android:textSize="40sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文本5"
            android:background="@color/teal_200"
            android:textColor="@color/white"
            android:textSize="40sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文本6"
            android:background="@color/purple_200"
            android:textColor="@color/white"
            android:textSize="40sp"
            />
    </TableRow>
</TableLayout>

三、GridLayout 网格布局

GridLayout和TableLayout(表格布局)有点类似,不过比TableLayout更加灵活好用。

  • android:orientation,指定布局方向,vertical-竖向布局,horizontal-横向布局
  • android:columnCount,设置总共多少列,默认没限制
  • android:rowCount,设置总共多少行,默认没限制
  • android:useDefaultMargins,true-如果组件没设置margin属性,则使用默认间距

子控件属性

  • android:layout_columnSpan,设置组件横跨多列
  • android:layout_rowSpan,设置组件横跨多行
  • 如果设置了多行或多列,且你要让组件填满横越过的行或列的话,需要添加下面这个属性: android:layout_gravity = "fill"
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:orientation="horizontal"
    android:rowCount="6"
    android:useDefaultMargins="true">

    <TextView
        android:layout_columnSpan="4"
        android:layout_gravity="fill"
        android:gravity="end"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#FFCCCC"
        android:text="0"
        android:textSize="50sp" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="回退" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:padding="10sp"
        android:text="清空" />

    <Button android:text="+" />
    <Button android:text="1" />
    <Button android:text="2" />
    <Button android:text="3" />
    <Button android:text="-" />
    <Button android:text="4" />
    <Button android:text="5" />
    <Button android:text="6" />
    <Button android:text="*" />
    <Button android:text="7" />
    <Button android:text="8" />
    <Button android:text="9" />
    <Button android:text="/" />
    <Button android:text="." />
    <Button android:text="0" />
    <Button android:text="=" />
</GridLayout>

四、RelativeLayout 相对布局

RelativeLayout 相对布局,可以相对父容器,也可以相对兄弟组件。

  • android:gravity,容器内部组件的排列方式(top,bottom,start,end,center),可以同时设置竖向横向两个竖向,用|隔开,比如end|top(左上方)
  • android:ignoreGravity,指定id的组件不受gravity影响

子控件属性

1)根据父容器定位

  • android:layout_alignParentStart,左对齐
  • android:layout_alignParentEnd,右对齐
  • android:layout_alignParentTop,上对齐
  • android:layout_alignParentBottom,下对齐
  • android:layout_centerHorizontal,水平居中
  • android:layout_centerVertical,垂直居中
  • android:layout_centerInParent,正中间

2)根据兄弟组件定位

  • android:layout_toStartOf,参考组件的左边
  • android:layout_toEndOf,参考组件的右边
  • android:layout_above,参考组件的上面
  • android:layout_below,参加组件的下面
  • android:layout_alignTop,对齐参考组件的上边界
  • android:layout_alignBottom,对齐参考组件的下边界
  • android:layout_alignStart,对齐参考组件的左边界
  • android:layout_alignEnd,对齐参考组件的右边界
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- 参照组件,在容器中间位置 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn01"
        android:textColor="@color/black"
        android:layout_centerInParent="true"
        android:text="参照物"/>

    <!-- 下方,且和参照物左对齐,如果控件大小一样,左右对齐效果是一样的 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/btn01"
        android:layout_above="@id/btn01"
        android:text="正上方"/>

    <!-- 上方,且和参照物左对齐,如果控件大小一样,左右对齐效果是一样的 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_toStartOf="@id/btn01"
        android:layout_alignTop="@id/btn01"
        android:text="左边"/>

    <!-- 右方,且和参照物下对齐,如果控件大小一样,上下对齐效果是一样的 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@id/btn01"
        android:layout_alignBottom="@id/btn01"
        android:text="右边"/>

    <!-- 右方,上方 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@id/btn01"
        android:layout_above="@id/btn01"
        android:text="右上方"/>

    <!-- 右方,下方 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@id/btn01"
        android:layout_below="@id/btn01"
        android:text="右下方"/>

    <!-- 左方,下方 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_toStartOf="@id/btn01"
        android:layout_below="@id/btn01"
        android:text="左下方"/>

    <!-- 左方,上方 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_toStartOf="@id/btn01"
        android:layout_above="@id/btn01"
        android:text="左上方"/>

    <!-- 下方,且和参照物左对齐 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/btn01"
        android:layout_below="@id/btn01"
        android:text="正下方"/>
</RelativeLayout>

相关推荐

Python入门学习记录之一:变量_python怎么用变量

写这个,主要是对自己学习python知识的一个总结,也是加深自己的印象。变量(英文:variable),也叫标识符。在python中,变量的命名规则有以下三点:>变量名只能包含字母、数字和下划线...

python变量命名规则——来自小白的总结

python是一个动态编译类编程语言,所以程序在运行前不需要如C语言的先行编译动作,因此也只有在程序运行过程中才能发现程序的问题。基于此,python的变量就有一定的命名规范。python作为当前热门...

Python入门学习教程:第 2 章 变量与数据类型

2.1什么是变量?在编程中,变量就像一个存放数据的容器,它可以存储各种信息,并且这些信息可以被读取和修改。想象一下,变量就如同我们生活中的盒子,你可以把东西放进去,也可以随时拿出来看看,甚至可以换成...

绘制学术论文中的“三线表”具体指导

在科研过程中,大家用到最多的可能就是“三线表”。“三线表”,一般主要由三条横线构成,当然在变量名栏里也可以拆分单元格,出现更多的线。更重要的是,“三线表”也是一种数据记录规范,以“三线表”形式记录的数...

Python基础语法知识--变量和数据类型

学习Python中的变量和数据类型至关重要,因为它们构成了Python编程的基石。以下是帮助您了解Python中的变量和数据类型的分步指南:1.变量:变量在Python中用于存储数据值。它们充...

一文搞懂 Python 中的所有标点符号

反引号`无任何作用。传说Python3中它被移除是因为和单引号字符'太相似。波浪号~(按位取反符号)~被称为取反或补码运算符。它放在我们想要取反的对象前面。如果放在一个整数n...

Python变量类型和运算符_python中变量的含义

别再被小名词坑哭了:Python新手常犯的那些隐蔽错误,我用同事的真实bug拆给你看我记得有一次和同事张姐一起追查一个看似随机崩溃的脚本,最后发现罪魁祸首竟然是她把变量命名成了list。说实话...

从零开始:深入剖析 Spring Boot3 中配置文件的加载顺序

在当今的互联网软件开发领域,SpringBoot无疑是最为热门和广泛应用的框架之一。它以其强大的功能、便捷的开发体验,极大地提升了开发效率,成为众多开发者构建Web应用程序的首选。而在Spr...

Python中下划线 ‘_’ 的用法,你知道几种

Python中下划线()是一个有特殊含义和用途的符号,它可以用来表示以下几种情况:1在解释器中,下划线(_)表示上一个表达式的值,可以用来进行快速计算或测试。例如:>>>2+...

解锁Shell编程:变量_shell $变量

引言:开启Shell编程大门Shell作为用户与Linux内核之间的桥梁,为我们提供了强大的命令行交互方式。它不仅能执行简单的文件操作、进程管理,还能通过编写脚本实现复杂的自动化任务。无论是...

一文学会Python的变量命名规则!_python的变量命名有哪些要求

目录1.变量的命名原则3.内置函数尽量不要做变量4.删除变量和垃圾回收机制5.结语1.变量的命名原则①由英文字母、_(下划线)、或中文开头②变量名称只能由英文字母、数字、下画线或中文字所组成。③英文字...

更可靠的Rust-语法篇-区分语句/表达式,略览if/loop/while/for

src/main.rs://函数定义fnadd(a:i32,b:i32)->i32{a+b//末尾表达式}fnmain(){leta:i3...

C++第五课:变量的命名规则_c++中变量的命名规则

变量的命名不是想怎么起就怎么起的,而是有一套固定的规则的。具体规则:1.名字要合法:变量名必须是由字母、数字或下划线组成。例如:a,a1,a_1。2.开头不能是数字。例如:可以a1,但不能起1a。3....

Rust编程-核心篇-不安全编程_rust安全性

Unsafe的必要性Rust的所有权系统和类型系统为我们提供了强大的安全保障,但在某些情况下,我们需要突破这些限制来:与C代码交互实现底层系统编程优化性能关键代码实现某些编译器无法验证的安全操作Rus...

探秘 Python 内存管理:背后的神奇机制

在编程的世界里,内存管理就如同幕后的精密操控者,确保程序的高效运行。Python作为一种广泛使用的编程语言,其内存管理机制既巧妙又复杂,为开发者们提供了便利的同时,也展现了强大的底层控制能力。一、P...