【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

相关文章

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要