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

本文主要是介绍【Android】三种常见的布局LinearLayout、GridLayout、RelativeLayout,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【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"><!-- 子视图居中对齐 --><Buttonandroid: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"><!-- 水平方向排列,使用权重分配空间 --><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="Button 1"/><Buttonandroid: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(填充父容器)。
  • 示例:
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, World!"/>
<Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Click Me"/>
5. background(背景)
  • 作用: 为视图设置背景颜色或背景图像。
  • 特点: 可以是颜色值(如 #RRGGBB)、图片资源(如 @drawable/bg_image)或者使用系统提供的样式。
  • 示例:
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button with Color Background"android:background="#FF5722"/><TextViewandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, LinearLayout!"android:textSize="20sp"/><Buttonandroid: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"><!-- 子视图按顺序排列,从左上角到右下角 --><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Row 0, Column 0"android:layout_row="0"android:layout_column="0"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Row 0, Column 1"android:layout_row="0"android:layout_column="1"/><ImageViewandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Row 1, Column 1"android:layout_row="1"android:layout_column="1"/><Buttonandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Center"android:layout_gravity="center"android:layout_row="0"android:layout_column="0"/><Buttonandroid: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"><TextViewandroid: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"/><Buttonandroid: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"><TextViewandroid: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"/><Buttonandroid: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"><TextViewandroid:id="@+id/textViewCenter"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Centered in Parent"android:layout_centerInParent="true"/><Buttonandroid: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"/><TextViewandroid: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)

这篇关于【Android】三种常见的布局LinearLayout、GridLayout、RelativeLayout的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068393

相关文章

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过