Android 布局系列(一):LinearLayout 使用指南

news2025/2/26 0:53:58

引言

在 Android 开发中,布局是每个应用的基础,而 LinearLayout 无疑是最常见、最简单的布局之一。它允许我们将多个视图按顺序排列,可以选择水平方向(horizontal)或垂直方向(vertical)。

LinearLayout 的简单性使其成为初学者和经验丰富的开发者都常用的布局之一,尤其适用于简单的视图排列需求。然而,在某些复杂的布局场景下,过度依赖 LinearLayout 可能会导致性能问题,因此了解如何高效使用它至关重要。在这篇文章中,我们将深入探讨 LinearLayout 的使用方法、常用属性以及一些最佳实践,帮助你更好地掌握这一强大的布局工具。

LinearLayout的基本概念

LinearLayout 的核心思想是“线性排列”,它让视图按特定的方向顺序排列,而不会考虑视图的其他布局关系。例如,它不会像 RelativeLayout 那样关注视图之间的相对位置,也不会像 ConstraintLayout 那样使用约束来控制布局。它的简洁性使其成为 Android 中最基础的布局之一。

LinearLayout的常见属性

LinearLayout 提供了一些关键属性,帮助我们定制其行为和子视图的布局方式。接下来我们将介绍几个常用的属性。

orientation

orientation 属性定义了 LinearLayout 中视图的排列方向。它决定了子视图的布局方向是水平方向还是垂直方向。

垂直排列:android:orientation="vertical"

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- 子视图 -->
</LinearLayout>
 

水平方向排列:android:orientation="horizontal"

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <!-- 子视图 -->
</LinearLayout>
 

gravity

gravity 属性控制 LinearLayout 本身的对齐方式,决定它的子视图如何相对于父容器对齐。常用的对齐方式包括:

  • center:居中。
  • start:左对齐。
  • end:右对齐。

例如,如果你希望将LinearLayout中的所有子视图垂直居中,可以使用:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <!-- 子视图 -->
</LinearLayout>
 

layout_weight 

layout_weight 属性是 LinearLayout 中非常关键的一个属性,它决定了子视图的相对空间比例。

layout_weight 的作用是让视图根据指定的权重来分配空间。通常与layout_width或者layout_height配合使用。指定为0dp,并通过layout_weight来决定视图的大小。

例如,以下代码演示了如何让两个TextView平分父容器的宽度:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Option 1" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Option 2" />
</LinearLayout>
 

LinearLayout 示例代码

这一部分,我们将会通过一些简单的示例代码,展示LinearLayout在不同场景下的应用。

简单的垂直排列

最基本的 LinearLayout 示例,将多个视图按垂直方向排列:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:layout_marginBottom="8dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_marginBottom="8dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</LinearLayout>

在这个例子中两个按钮和输入框会居中垂直排列,效果如下:

使用layout_weight 分配空间

layout_weight 属性非常适用于需要按照比例分配空间的场景,下面一个案例展示如使用layout_weight评分父容器的空间。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/button1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:layout_marginBottom="8dp" />

    <Button
        android:id="@+id/button2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_marginBottom="8dp" />

    <Button
        android:id="@+id/button3"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 3"
        android:layout_marginBottom="8dp" />

</LinearLayout>

这三个Button将会平分父容器的宽度,因为它们的layout_weight都是1,且layout_width设置为0dp表示由权重决定宽度,效果如下:

嵌套 LinearLayout 实现复杂布局

更多的时候,我们需要将多个布局进行嵌套,来构建更复杂的布局,以下是一个嵌套布局的示例。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:layout_marginBottom="8dp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 2"
            android:layout_marginBottom="8dp" />

    </LinearLayout>
    <Button
        android:id="@+id/button3"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 3"
        android:layout_marginBottom="8dp" />

</LinearLayout>

外部的LinearLayout 使用水平排列,内容水平垂直居中,而内部的LinearLayout采用垂直排列,两个按钮上下排列,效果如下:

结语

在这篇文章中,我们深入探讨了 LinearLayout 布局的基本概念和常用属性,并通过实际示例展示了如何在不同场景下使用它。LinearLayout 以其简单直观的排列方式,成为了 Android 开发中最常用的布局之一。无论是垂直排列还是水平排列,或者利用layout_weight进行空间分配,LinearLayout 都为开发者提供了灵活且高效的方式来组织视图。

虽然它在很多简单场景中非常适用,但在需要更复杂视图关系时,可能需要结合其他布局类型来满足需求。因此,在实际开发中,合理选择布局,平衡性能和可维护性是至关重要的。

希望这篇文章能够帮助你更好地掌握 LinearLayout,提升你的布局设计能力。如果你有任何问题,或者希望进一步探讨,欢迎随时留言与我交流!

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

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

相关文章

Android级联选择器,下拉菜单

近期android开发&#xff0c;遇到的需求&#xff0c;分享二个android可能用到的小组件 下拉选择器&#xff1a;它的实现&#xff0c;主要是需要监听它依附的组件当前距离屏幕顶端的位置。 在显示下拉菜单中&#xff0c;如果需要点击上面有响应。可通过activity拿到decorview(ac…

【每日八股】MySQL篇(一):概述

关系的三个范式是什么&#xff1f; 第一范式&#xff08;1NF&#xff09;&#xff1a;用来确保每列的原子性&#xff0c;要求每列都是不可再分的最小数据单元。 概括&#xff1a;表中的每一列都是不可分割的最小原子值&#xff0c;且每一行都是唯一的。 第二范式&#xff08…

Remainder Problem CF1207F

题目&#xff1a;题目链接 题目大意 题目描述 给你一个长度为 500000 的序列&#xff0c;初值为 0 &#xff0c;你要完成 q 次操作&#xff0c;操作有如下两种&#xff1a; 1 x y : 将下标为 x 的位置的值加上 y2 x y : 询问所有下标模 x 的结果为 y 的位置的值之和 输入格…

SpringBoot之自定义简单的注解和AOP

1.引入依赖 <!-- AOP依赖--> <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.8</version> </dependency>2.自定义一个注解 package com.example.springbootdemo3.an…

自由学习记录(38)

python语法 def def print_receipt (store_name, items, total_price, cashier"Self-Checkout", payment_method"Credit Card"): Python 的 函数定义 语法 def print_receipt(...) → 定义了一个名为 print_receipt 的函数。store_name, items, total_…

【SQL实验】触发器

下载素材文件”tsgl”、“成绩管理”,将tsgl.bak和成绩管理.bak数据库还原到库中【导入操作在之前的文章中详细讲过】 触发器 1、为图书表设置更新触发器&#xff0c;根据总编号来更新书名、作者、出版社、分类号和单价(根据总编号找到相应记录&#xff0c;然后更新书名、作者…

CPU多级缓存机制

目录 一、前置知识 ---- CPU的核心 1.1. 单核与多核CPU 二、CPU多级缓存机制 三. 缓存的基本结构/缓存的存储结构 四、CPU缓存的运作流程/工作原理 五、CPU多级缓存机制的工作原理【简化版】 5.1. 缓存访问的过程 (5.1.1) L1缓存&#xff08;一级缓存&#xff09;访问 …

神经网络八股(3)

1.什么是梯度消失和梯度爆炸 梯度消失是指梯度在反向传播的过程中逐渐变小&#xff0c;最终趋近于零&#xff0c;这会导致靠前层的神经网络层权重参数更新缓慢&#xff0c;甚至不更新&#xff0c;学习不到有用的特征。 梯度爆炸是指梯度在方向传播过程中逐渐变大&#xff0c;…

SmartMediakit之音视频直播技术的极致体验与广泛应用

引言 在数字化时代&#xff0c;音视频直播技术已经深入到各个行业和领域&#xff0c;成为信息传递和交流的重要手段。视沃科技自2015年成立以来&#xff0c;一直致力于为传统行业提供极致体验的音视频直播技术解决方案&#xff0c;其旗下的大牛直播SDK凭借强大的功能和卓越的性…

【R包】tidyplots----取代ggplot2的科研绘图利器

文章目录 介绍安装Usage文档参考 介绍 tidyplots----取代ggplot2的科研绘图利器。tidyplots的目标是简化为科学论文准备出版的情节的创建。它允许使用一致和直观的语法逐渐添加&#xff0c;删除和调整情节组件。 安装 You can install the released version of tidyplots fro…

DeepSeek 15天指导手册——从入门到精通 PDF(附下载)

DeepSeek使用教程系列--DeepSeek 15天指导手册——从入门到精通pdf下载&#xff1a; https://pan.baidu.com/s/1PrIo0Xo0h5s6Plcc_smS8w?pwd1234 提取码: 1234 或 https://pan.quark.cn/s/2e8de75027d3 《DeepSeek 15天指导手册——从入门到精通》以系统化学习路径为核心&…

微信小程序实现拉卡拉支付

功能需求&#xff1a;拉卡拉支付&#xff08;通过跳转拉卡拉平台进行支付&#xff09;&#xff0c;他人支付&#xff08;通过链接进行平台跳转支付&#xff09; 1.支付操作 //支付 const onCanStartPay async (obj) > {uni.showLoading({mask: true})// 支付接口获取需要传…

Unity 第三人称人物切动画时人物莫名旋转

前提: 使用Starter Asset包中的第三人称插件包. 在给3D角色的动画器增加新动画时, 发现进入新动画会让角色莫名转动. 观察后发现是动画强行将朝向掰"正", 人物动画在进行时朝向会一直变化, 这使得动作非常的怪异. 对系动画进行以下处理后, 将可以解决这种不自然: 选…

启动Redis报错记录

突然启动Redis就报了个错&#xff1a;‘Could not create server TCP listening socket 127.0.0.1:6379: bind: 操作成功完成。‘ 查了下解决方案&#xff0c;应该是6379端口已绑定&#xff0c;服务没有关闭。 需要输入命令redis-cli 再输入shutdown 但又出现了新的问题&…

自然语言处理NLP 04案例——苏宁易购优质评论与差评分析

上一篇文章&#xff0c;我们爬取了苏宁易购平台某产品的优质评价和差评&#xff0c;今天我们对优质评价与差评进行分析 selenium爬取苏宁易购平台某产品的评论-CSDN博客 目录 1. 数据加载 2. 中文分词 3. 停用词处理 4. 数据标注与合并 5. 数据集划分 6. 文本特征提取 …

图片爬取案例

修改前的代码 但是总显示“失败” 原因是 修改之后的代码 import requests import os from urllib.parse import unquote# 原始URL url https://cn.bing.com/images/search?viewdetailV2&ccidTnImuvQ0&id5AE65CE4BE05EE7A79A73EEFA37578E87AE19421&thidOIP.TnI…

官方文档学习TArray容器

一.TArray中的元素相等 1.重载一下 元素中的 运算符&#xff0c;有时需要重载排序。接下来&#xff0c;我们将id 作为判断结构体的标识。 定义结构体 USTRUCT() struct FXGEqualStructInfo {GENERATED_USTRUCT_BODY() public:FXGEqualStructInfo(){};FXGEqualStructInfo(in…

Web刷题之PolarDN(中等)

1.到底给不给flag呢 代码审计 一道典型的php变量覆盖漏洞 相关知识 什么是变量覆盖漏洞 自定义的参数值替换原有变量值的情况称为变量覆盖漏洞 经常导致变量覆盖漏洞场景有&#xff1a;$$使用不当&#xff0c;extract()函数使用不当&#xff0c;parse_str()函数使用不当&…

学习笔记-250222

论文&#xff1a; Learning Hierarchical Prompt with Structured Linguistic Knowledge for Vision-Language Models 主要研究llm在图像分类中的能力&#xff0c;当提示输入目标类别时&#xff0c;llm能够生成相关的描述以及相应的结构化关系。 1.首先利用llm从普通的描述中获…

Unity游戏制作中的C#基础(1)界面操作基础

1.脚本有关注意事项 &#xff08;1&#xff09;.进入项目之后&#xff0c;一般创建一个文件夹Scripts用来存放c#脚本&#xff1b; &#xff08;2&#xff09;.在Scripts中创建脚本&#xff0c;双击脚本&#xff0c;进入VS编辑器&#xff0c;有如下结构&#xff1a; start&#…