【Android】三种常见的布局LinearLayout、GridLayout、RelativeLayout

news2024/10/6 20:35:34

【Android】三种常见的布局LinearLayout、GridLayout、RelativeLayout

在 Android 开发中,布局(Layout)是构建用户界面的基础。通过合理的布局管理,可以确保应用在不同设备和屏幕尺寸上都能有良好的用户体验。本文将简单介绍 Android 中的三种常见布局管理器:线性布局(LinearLayout)、网格布局(GridLayout)和相对布局(RelativeLayout)。

1. LinearLayout

LinearLayout 是一种简单的布局管理器,它按照水平或垂直方向排列子视图。

基础属性和特点:

在 Android 开发中,常见的布局属性包括方向(orientation)、对齐方式(gravity)、权重(weight)、布局宽度(layout_width)、背景(background)。以下是详细解释和多个示例,展示它们的用法和特点。

1. orientation(方向)
  • 作用: 确定布局中子视图的排列方向。
  • 特点: 可以设置为 horizontal(水平)或 vertical(垂直),影响子视图的排列方式。
2. gravity(对齐方式)
  • 作用: 控制子视图在布局中的对齐方式。
  • 特点: 可以设置为 topbottomcenter 等,使子视图相对于布局的位置对齐。
  • 示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <!-- 子视图居中对齐 -->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Centered Button"/>

</LinearLayout>

image-20240616180335480

3. weight(权重)
  • 作用: 控制子视图在剩余空间中的分配比例。
  • 特点: 可以用来实现弹性布局,使得某些子视图占据更多的空间。
  • 示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- 水平方向排列,使用权重分配空间 -->

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 1"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Button 2"/>

</LinearLayout>

image-20240616180424176

4. layout_width(布局宽度)
  • 作用: 设置子视图的宽度。
  • 特点: 可以设置为 wrap_content(根据内容自适应)或 match_parent(填充父容器)。
  • 示例:
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, World!"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click Me"/>
5. background(背景)
  • 作用: 为视图设置背景颜色或背景图像。
  • 特点: 可以是颜色值(如 #RRGGBB)、图片资源(如 @drawable/bg_image)或者使用系统提供的样式。
  • 示例:
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button with Color Background"
    android:background="#FF5722"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView with Background Image"
    android:background="@drawable/bg_pattern"/>

image-20240616180534431

示例代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, LinearLayout!"
        android:textSize="20sp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"/>

</LinearLayout>

image-20240616180629439

权重(Weight)的概念

在 LinearLayout 中,权重(weight)是一种布局属性,用于控制子视图在剩余空间中的分配比例。具体来说:

  • android

    属性允许您为每个子视图分配一个权重值,这个值决定了子视图在剩余空间中所占的比例。

  • 当布局的尺寸是确定的(例如 match_parent 或具体的尺寸)时,布局管理器会计算所有没有指定尺寸的子视图的权重总和(例如 layout_widthlayout_height 设置为 0dp),然后按照各自的权重值来分配剩余空间。

2. GridLayout

GridLayout 是 Android 中用于实现网格布局的强大工具,允许开发者以行和列的方式排列子视图。

1. rowCount 和 columnCount
  • 作用: 分别指定 GridLayout 的行数和列数,用于确定网格布局的大小和结构。
  • 示例:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3">

    <!-- 子视图按顺序排列,从左上角到右下角 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Row 0, Column 0"
        android:layout_row="0"
        android:layout_column="0"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Row 0, Column 1"
        android:layout_row="0"
        android:layout_column="1"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image"
        android:layout_row="0"
        android:layout_column="2"/>

    <!-- 添加更多子视图,填满网格 -->
</GridLayout>

image-20240616180806573

2. layout_row 和 layout_column
  • 作用: 指定子视图在 GridLayout 中的行和列位置。
  • 示例:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Row 1, Column 1"
        android:layout_row="1"
        android:layout_column="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Row 2, Column 0"
        android:layout_row="2"
        android:layout_column="0"/>

    <!-- 添加更多子视图,根据需要设置 layout_row 和 layout_column -->
</GridLayout>

image-20240616180911115

3. layout_gravity
  • 作用: 控制子视图在其网格单元格中的对齐方式,类似于 LinearLayout 的 gravity 属性。
  • 示例:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    android:rowCount="2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Center"
        android:layout_gravity="center"
        android:layout_row="0"
        android:layout_column="0"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bottom-Right"
        android:layout_gravity="bottom|right"
        android:layout_row="1"
        android:layout_column="1"/>

</GridLayout>

image-20240616180930421

4. layout_rowSpan 和 layout_columnSpan
  • 作用: 设置子视图在 GridLayout 中跨越的行数和列数,使其占据多个网格单元格。
  • 示例:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Span 2 Rows"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_rowSpan="2"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Span 3 Columns"
        android:layout_row="1"
        android:layout_column="1"
        android:layout_columnSpan="3"/>

</GridLayout>

3. RelativeLayout

RelativeLayout 是 Android 中一种灵活的布局管理器,允许开发者通过相对位置来布局子视图。以下详细介绍 RelativeLayout 的基础属性和特点,并提供多个示例演示其灵活性和应用场景。

1. layout_alignParentTop, layout_alignParentBottom, layout_alignParentLeft, layout_alignParentRight
  • 作用: 将子视图相对于父视图的顶部、底部、左侧、右侧进行对齐。
  • 示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aligned to Parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aligned to Bottom Right"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

在这个示例中,TextView 设置了 layout_alignParentTop="true"layout_alignParentLeft="true",使其分别相对于父视图的顶部和左侧对齐。Button 设置了 layout_alignParentBottom="true"layout_alignParentRight="true",使其分别相对于父视图的底部和右侧对齐。

image-20240616181021092

2. layout_above, layout_below, layout_toLeftOf, layout_toRightOf
  • 作用: 将子视图相对于其他子视图的位置进行对齐。
3. layout_centerInParent, layout_centerHorizontal, layout_centerVertical
  • 作用: 将子视图在父视图中居中对齐。
  • 示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textViewCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Centered in Parent"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/buttonCenterHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Centered Horizontally"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/textViewCenter"/>

    <TextView
        android:id="@+id/textViewCenterVertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Centered Vertically"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/buttonCenterHorizontal"/>

</RelativeLayout>

TextView textViewCenter 使用 layout_centerInParent="true" 居中于父视图中心。Button buttonCenterHorizontal 使用 layout_centerHorizontal="true" 水平居中,且位于 textViewCenter 的下方。TextView textViewCenterVertical 使用 layout_centerVertical="true" 垂直居中,且位于 buttonCenterHorizontal 的右侧。

image-20240616181200369

总结

以上是 LinearLayout、GridLayout 和 RelativeLayout 的基础知识和使用示例。

参考:

2.2.5 GridLayout(网格布局) | 菜鸟教程 (runoob.com)

2.2.2 RelativeLayout(相对布局) | 菜鸟教程 (runoob.com)

2.2.1 LinearLayout(线性布局) | 菜鸟教程 (runoob.com)

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

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

相关文章

Premiere Pro 关键帧的运用(热气球飞行)

制作“热气球飞行”效果&#xff0c;步骤如下&#xff1a; 1.新建项目>新建序列&#xff0c;项目名称为“热气球飞行”&#xff0c;序列设置如下图所示。 2.将“背景”和“飞船”素材导入项目面板中&#xff0c;并将“背景”素材拖到时间线的视频轨道v1上&#xff0c;将“飞…

如何下载Visual Studio2022编译器。详细教程

朝三暮四就该死&#xff01; --莫迪大仙 引言&#xff1a;今天下个Visual Studio给大家也分享分享&#xff0c;同时怕自己忘记&#xff0c;也记录记录。&#xff08;windows版本演示&#xff09; 一、下载网址 网址&#xff1a;Visual Studio2022下载 - Windows、Mac、Linux…

Leetcode Hot100之双指针

1. 移动零 题目描述 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数组进行操作。解题思路 双指针遍历一遍即可解决: 我们定义了两个指针 i 和 j&#xf…

scratch编程03-反弹球

这篇文章和上一篇文章《scratch3编程02-使用克隆来编写小游戏》类似&#xff08;已经完全掌握了克隆的可以忽略这篇文章&#xff09;&#xff0c;两篇文章都使用到了克隆来编写一个小游戏&#xff0c;这篇文章与上篇文章不同的是&#xff0c;本体在进行克隆操作时&#xff0c;不…

图卷积网络(Graph Convolutional Network, GCN)

图卷积网络&#xff08;Graph Convolutional Network, GCN&#xff09;是一种用于处理图结构数据的深度学习模型。GCN编码器的核心思想是通过邻接节点的信息聚合来更新节点表示。 图的表示 一个图 G通常表示为 G(V,E)&#xff0c;其中&#xff1a; V 是节点集合&#xff0c;…

【单片机】三极管的电路符号及图片识别

一&#xff1a;三极管的电路符号 二&#xff1a;三极管的分类 a;按频率分&#xff1a;高频管和低频管 b;按功率分&#xff1a;小功率管&#xff0c;中功率管和的功率管 c;按机构分&#xff1a;PNP管和NPN管 d;按材质分&#xff1a;硅管和锗管 e;按功能分&#xff1a;开关管和放…

Python基础-引用参数、斐波那契数列、无极分类

1.引用参数的问题 &#xff08;1&#xff09;列表&#xff08;list&#xff09; 引用参数&#xff0c;传地址的参数&#xff0c;即list1会因list2修改而改变。 list1 [1,2,3,4] list2 list1 print(list1) list2[2] 1 print(list2) print(list1)非引用参数&#xff0c;不传…

获得淘宝app商品详情原数据API接口|商品价格详情页面优惠券主图

item_get_app&#xff1a;通过商品id获取商品详情页数据 注册账号获取API测试地址 公共参数 名称类型必须描述keyString是调用key&#xff08;必须以GET方式拼接在URL中&#xff09;secretString是调用密钥api_nameString是API接口名称&#xff08;包括在请求地址中&#xf…

如何将本地代码上传到git上面

精简步骤&#xff1a; git init git add . git commit -m "first init" git remote add origin 远程仓库地址 git push -u origin master 目录 1、初始化本地仓库 2、添加所有文件到本地仓库 3、提交更改到本地仓库 4、添加github仓库作为远程仓库 5、推送更改…

简单介绍vim

文章目录 前言一、Vim的特点二、安装Vim三、设置Vim配置文件的位置&#xff1a;编辑配置文件&#xff1a;添加配置选项&#xff1a;保存并退出编辑器&#xff1a;快速配置验证设置&#xff1a; 总结 前言 Vim是一款强大的文本编辑器&#xff0c;被广泛用于各种编程和文本编辑任…

Gin 详解

Gin 介绍 gin框架是一个基于go语言的轻量级web框架&#xff0c;它具有高效性、灵活性、易扩展性路由 gin框架使用的是定制版的httprouter 其路由原理是大量使用公共前缀的树结构&#xff0c;注册路由的过程就是构造前缀树的过程。 具有公共前缀的节点也共享一个公共父节点。…

若依 ruoyi 显示隐藏搜索框 显示隐藏列

一、 显示隐藏搜索框 页面搜索关键字 showSearch&#xff0c;设置是否显示 隐藏&#xff1a; 显示&#xff1a; 二、自定义设置 显示隐藏列 1. 页面搜索关键字 right-toolbar&#xff0c;新增&#xff1a; :columns"columns" 2. js下 data(){return{}}中新增&am…

手把手教你创建并启动一个Vue3项目(Windows版)

一、Node安装 1、下载地址&#xff1a;Node.js — Run JavaScript Everywhere 2、安装Node&#xff0c;双击启动一直Next 3、验证安装Node是否成功&#xff0c;打开CMD命令窗口&#xff0c;输入node -v&#xff0c;显示版本就表示成功 4、验证安装npm是否成功&#xff0c;npm是…

DFS(一)

问题一(指数级选择) 从1~n这n个整数中任意选取多个&#xff0c;输出所有可能的选择方案。 首先想一下&#xff0c;在现实世界中&#xff0c;我们要如何解决这个问题。 应该是一个一个枚举&#xff0c;即每个数都可以有两个选择(选/不选)。共有种结果。 想一下&#xff0c;如…

UI设计速成课:理解模态窗口与非模态窗口的区别

我们日常所说的弹性框架是非常笼统的概念。我们习惯性地称之为对话框架、浮动层和提示条。弹性框架可以分为两种:模态弹性框架和非模态弹性框架。产品需要弹性框架来传递信息&#xff0c;用户需要弹性框架来接受反馈&#xff0c;但是没有经过推敲的弹出窗口设计很容易让用户感到…

mouseinc-smartUp Gestures被禁用后的替代品

前言 smartUp Gestures恶意软件,既然谷歌这么判断,可能大概率没错了,我们换一个mouseInc吧下载地址 https://www.123pan.com/s/fDzUVv-hCtlA 设置下会更好用 设置 通过AHK设置下一些快捷操作~ 对应的查找 https://source.chromium.org/chromium/chromium/src//main:chrome/a…

【CT】LeetCode手撕—20. 有效的括号

题目 原题连接&#xff1a;20. 有效的括号 1- 思路 模式识别 模式1&#xff1a;括号左右匹配 ——> 借助栈来实现 ——> Deque<Character> deque new LinkedList<>()模式2&#xff1a;顺序匹配 ——> 用 if 判断 具体思路 1.遇到左括号 直接入栈相应…

【Java面试】二十二、JVM篇(下):JVM参数调优与排查

文章目录 1、JVM的参数在哪里设置2、常见的JVM调优参数有哪些3、常见的JVM调优工具有哪些4、Java内存泄漏的排查思路5、CPU飙高的排查思路 1、JVM的参数在哪里设置 war包部署&#xff0c;在tomcat中设置&#xff0c;修改TOMCAT_HOME/bin/catalina.sh 文件 jar包启动&#xff0…

华为IPD体系中三大流程之IPD流程的六个阶段和七个评审点介绍

概念 IPD集成产品开发&#xff0c;英文是IntegratedProduct Development&#xff0c;是一整套科学的研发创新管理方法论&#xff0c;将产品经营管理思想和理念置入到新产品开发和产品管理过程中&#xff0c;因此IPD是不仅是一套研发管理体系&#xff0c;更是一套产品经营管理体…

Z世代职场价值观的重塑:从“班味”心态到个人成长的追求

近日&#xff0c;社交平台Soul APP联合上海市精神卫生中心&#xff08;俗称“宛平南路600号”&#xff09;发布《2024年Z世代职场心理健康报告》&#xff08;下称“报告”&#xff09;&#xff0c;发现今天的年轻人正以其独特的价值观和行为模式&#xff0c;重新定义成功与成就…