Android常用布局总结之(LinearLayout、TableLayout、GridLayout、RelativeLayout)

news2025/2/1 14:47:29

一、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:layout_height 设置为 “0dp”(针对垂直布局),或将每个视图的 android:layout_width 设置为 “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的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!! 但是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>

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/104378.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

在线点餐网站

开发工具(eclipse/idea/vscode等)&#xff1a; 数据库(sqlite/mysql/sqlserver等)&#xff1a; 功能模块(请用文字描述&#xff0c;至少200字)&#xff1a; 管理员&#xff1a; 1、管理门店介绍、联系我们 2、对公告类型、公告信息增删改查 3、对菜品类型、菜品信息增册改查 4…

【计算机考研408】2023考研408相关题目预测总结

目录数据结构选择1-时间复杂度选择2-栈或者队列选择3-二叉树、树、森林选择4-并查集选择5-红黑树选择6-图的概念选择7-图的应用选择8-B树&#xff08;B-树&#xff09;选择8-B树选择8-B树与B树的区别选择8-B树与B树的相关应用选择9-查找算法选择10、11-排序算法综合应用题41-算…

UG/NX二次开发Siemens官方NXOPEN实例解析—2.3 Selection_UIStyler

列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析—2.1 AssemblyViewer UG/NX二次开发Siemens官方NXOPEN实例解析—2.2 Selection UG/NX二次开发Siemens官方NXOPEN实例解析—2.3 Selection_UIStyler 列文章目录 文章目录 前言 一、知识点提取 二、案例需求分析 三、…

大数据期末总结

文章目录一、这学期分别接触了Linux&#xff0c;hadoop&#xff0c;hbase&#xff0c;hive1、Linux2、Hadoop3、hbase4、hive二、总结一、这学期分别接触了Linux&#xff0c;hadoop&#xff0c;hbase&#xff0c;hive 1、Linux Linux是一款安全性十分良好的操作系统。不仅有用…

前端监控与前端埋点方案

前端监控与前端埋点方案 https://blog.csdn.net/sinat_36521655/article/details/114650138 ​ 用户行为数据可以通过前端数据监控的方式获得&#xff0c;除此之外&#xff0c;前端还需要实现**性能监控和异常监控。**性能监控包括首屏加载时间、白屏时间、http请求时间和htt…

软件设计师

1.在项目初期的需求并不明确&#xff0c;需要不断同用户进行交流与沟通&#xff0c;分布获取功能要求&#xff0c;在这种情况要采用敏捷开发方法最适合&#xff0c;比如极限编程 2.设计模式包括&#xff1a;创建型&#xff0c;结构型&#xff0c;行为型三大类别。 创建型模式…

UnRaid添加镜像源加速应用安装的正确方法

文章目录0、前言1、寻找适合你网络的最优镜像源1.1、首先点击下图示红框处进入UnRaid的终端1.2、输入如下代码检测每一个镜像源的速度&#xff1a;2、更改镜像源方法2.1、修改Go文件方法2.2、用户自定义脚本方式2.2.1、安装User Scripts插件2.2.2、在User Scripts插件中添加更改…

【自动驾驶环境感知项目】——基于Paddle3D的点云障碍物检测

文章目录1. 自动驾驶实战&#xff1a;基于Paddle3D的点云障碍物检测1.1 环境信息1.2 准备点云数据1.3 安装Paddle3D1.4 模型训练1.5 模型评估1.6 模型导出1.7 模型部署效果1. 自动驾驶实战&#xff1a;基于Paddle3D的点云障碍物检测 项目地址——自动驾驶实战&#xff1a;基于P…

已经拍好的视频怎么加水印?视频加水印方法大分享

现在不管是网课视频&#xff0c;还是一些视频博主自制的vlog&#xff0c;我们都可以在这些视频里面看到水印&#xff0c;它不仅可以防止他人盗用视频&#xff0c;还可以作为自己形象的宣传&#xff0c;吸引流量。不过现在还是有很多小伙伴不知道怎么给视频添加水印。别急&#…

js将图片url转化为base64

将以下代码复制封装于xxx.js文件中&#xff0c;放置在项目文件夹utiles下 /*** 把url转换为 canvas对象* param url 网络图片地址必须服务器设置允许跨域* returns {Promise<any>}*/ export default function urlToCanvas (url) {return new Promise((resolve) > {var…

2022卡塔尔世界杯 | 我与足球的爱恨情仇

超燃世界杯&#xff0c;决战卡塔尔⚽我与足球在生活上的交集一、小学二、中学三、大学&#x1f4bb;我与足球在技术上的碰撞一、与足球有关的题目训练二、使用Java代码做一个足球小游戏&#x1f3c6;2022卡塔尔世界杯冠军 —— 阿根廷yyds一、球队比赛过程二、热门球员介绍三、…

小学生C++编程基础 课程6(共9题)

Go C编程 第1课 神奇的魔笔 Go C编程 第1课 神奇的魔笔_dllglvzhenfeng的博客-CSDN博客_goc编程作品 GoC2018下册 第2课&#xff08;C画图&#xff09; GoC2018下册 第2课&#xff08;C画图&#xff09;_dllglvzhenfeng的博客-CSDN博客 Go C 编程 第3课 魔法自动机 Go C 编程…

C++:类和对象:继承

前言&#xff1a; 继承时面向对象额三大特性之一&#xff1a; 在面向对象中&#xff0c;有些类与类之间存在特殊关系&#xff0c;下级别的类除了拥有上一级别的共性&#xff0c;还有自己的特性&#xff0c;这个时候我们就需要考虑利用继承的技术减重复代码。 1&#xff1a;继承…

Redisson分布式锁

Redisson分布式锁 Redisson 是什么&#xff1f; Redisson是一个Java库&#xff0c;它为Redis服务器提供分布式和可扩展的Java对象和服务&#xff08;Set、Multimap、SortedSet、Map、Lock、Semaphore、CountDownLatch、Publish/Subscribe、Bloom filter等&#xff09;。它允许…

工控安全-S7协议

文章目录一、西门子PLC系统构成二、S7协议结构三、TPKT协议四、COTP协议4.1 COTP连接包4.2 COTP功能包五、S7Comm协议5.1 头(Header)5.2 作业请求(Job)和确认数据响应(Ack_Data)5.2.1 建立通信(Setup communication [0xF0])5.2.2 读取值(Read Var [0x04])5.2.2.1 当PDU为JOB时5…

redis之分片集群

0. 前言 在海量的数据面前&#xff0c;单个 redis 实例的能力是有限的&#xff0c;无可能无限增大的内存&#xff0c;所以必须要构建分片集群&#xff0c;来横向拓展来支持保存更多的数据。 1. 分片集群是什么&#xff1f; 分片集群主要是将 redis 的数据划分成多份&#xf…

BHG Mall 聚焦消费者需求,“超级宠粉节”缔造营销新高度

“如何读懂消费者&#xff1f;”处在行业关键转型期的购物中心&#xff0c;面临着这一待解的难题。 有哪些业态、品牌、商品让消费者着迷又上瘾&#xff1f;购物中心需要培养和消费者之间的默契&#xff0c;购物中心不仅要引领消费趋势、满足消费需求&#xff0c;还要深度融入…

Java+SSM宠物销售网站(含源码+论文+答辩PPT等)

项目功能简介: 该项目采用的技术实现如下&#xff1a; 后台框架&#xff1a;Spring、SpringMVC、MyBatis UI界面&#xff1a;JSP、jQuery 数据库&#xff1a;MySQL 该系统主要分为前台和后台两大功能模块&#xff0c;共包含两个角色&#xff1a;用户、管理员。 具体的系统功能如…

高分子PEG:DBCO-PEG-OPSS,OPSS-PEG-DBCO,二苯并环辛烯PEG邻吡啶二硫

【产品描述】 DBCO-PEG-OPSS中DBCO试剂可以自动标记叠氮化物修饰的生物分子&#xff0c;而不需要使用有毒的铜催化剂。聚乙二醇化可以提高多肽和蛋白质的溶解性和稳定性&#xff0c;降低其免疫原性。它还可以抑制带电分子与修饰表面的非特定结合。西安凯新生物科技有限公司点击…

html5:notification(浏览器通知)

一、notification简介 Web Notifications是HTML5 的一个特性&#xff0c;目前我知道的有谷歌浏览器和windows edge对它进行了支持&#xff0c;用于向用户配置和显示桌面通知。 二、notification方法 2.1静态方法 这些方法仅在 Notification 对象中有效。 Notification.requ…