【Android从零单排系列二十九】《Android布局介绍——LinerLayout》

目录

前言

一 LinerLayout基本介绍

二 LinerLayout使用方法

三 LinerLayout常见属性及方法

四 LinerLayout简单案例

五 总结


前言

小伙伴们,在前面的系列文章中,我们重点介绍了Android开发中用到的视图组件,从本文开始我们继续盘点Android中的布局,本文主要介绍一下LinerLayout。

一 LinerLayout基本介绍

LinearLayout(线性布局)是一种在Android中常用的布局管理器,用于在水平或垂直方向上排列子视图。它可以作为容器来包含其他视图组件,并根据指定的布局属性进行排列。

二 LinerLayout使用方法

  1. 在XML布局文件中定义LinearLayout
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        <!-- 子视图元素 -->
        
    </LinearLayout>
    

    在上述代码中,我们创建了一个垂直方向的LinearLayout,并将其宽度设置为与父视图相匹配(match_parent),高度根据子视图自适应(wrap_content)。

  2. 添加子视图元素: 在LinearLayout标签内部添加其他视图组件作为其子元素,例如TextView、Button等。根据需要可以使用不同的布局参数来控制子视图的大小和对齐方式。
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, world!" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click Me" />
            
    </LinearLayout>
    

    在上述代码中,我们在LinearLayout中添加了一个TextView和一个Button作为子视图。

  3. 设置布局属性: 可以通过在每个子视图的布局参数中设置不同的属性来控制子视图在LinearLayout中的位置和大小,例如android:layout_weight属性可以用来设置子视图的权重,实现按比例分配剩余空间。
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Hello, world!" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:text="Click Me" />
            
    </LinearLayout>
    

    在上述代码中,我们将TextView和Button的高度都设置为0dp,并使用android:layout_weight属性将剩余的空间按比例分配给它们。在这种情况下,Button的权重是TextView的两倍,所以Button会占据

以下是LinearLayout的特点和使用方法的总结:

  1. 方向:LinearLayout可以在水平方向(horizontal)或垂直方向(vertical)上排列子视图。
  2. 排列方式:子视图可以按照添加的顺序依次排列(默认),也可以根据权重(weight)或布局权重(layout_weight)进行分配空间和对齐。
  3. 布局属性:通过在子视图的布局参数中设置不同的权重、对齐方式和填充方式,可以灵活控制每个子视图在LinearLayout中的位置和大小。
  4. 嵌套:可以嵌套多个LinearLayout以实现更复杂的布局结构。
  5. 大小测量:LinearLayout会根据子视图的测量要求和布局参数来计算自身的大小和子视图的位置。

使用LinearLayout时,可以考虑以下几点:

  1. 在XML布局文件中使用<LinearLayout>标签来定义LinearLayout
  2. 设置android:orientation属性为"horizontal"或"vertical"来指定水平或垂直布局。
  3. 可以使用android:layout_width和android:layout_height属性来设置LinearLayout的宽度和高度。
  4. LinearLayout中添加子视图(如Button、TextView等)作为其子元素,并使用布局参数(layout_width和layout_height等)设置每个子视图的大小和对齐方式。
  5. 可以使用android:layout_weight属性在LinearLayout中对子视图进行权重分配,实现灵活的空间占用和对齐。

三 LinerLayout常见属性及方法

方法:

  1. setOrientation(int orientation):设置LinearLayout的排列方向。

  2. addView(View view, ViewGroup.LayoutParams params):将子视图添加到LinearLayout中。

  3. setWeightSum(float weightSum):设置LinearLayout中权重的总和。

  4. setGravity(int gravity):设置LinearLayout内部子视图的对齐方式。

  5. setBaselineAligned(boolean aligned):设置是否按基线对齐子视图。

  6. setBaselineAlignedChildIndex(int index):设置按基线对齐时参考的子视图索引。

  7. setDividerDrawable(Drawable drawable):设置LinearLayout的分隔线Drawable。

  8. setShowDividers(int showDividers):设置是否显示分隔线以及显示的位置。

  9. setDividerPadding(int padding):设置分隔线的间距。

属性:

  1. android:orientation:设置LinearLayout的排列方向。

  2. android:layout_weight:设置子视图的权重。

  3. android:gravity:设置LinearLayout内部子视图的对齐方式。

  4. android:baselineAligned:设置是否按基线对齐子视图。

  5. android:baselineAlignedChildIndex:设置按基线对齐时参考的子视图索引。

  6. android:divider:设置LinearLayout的分隔线Drawable。

  7. android:showDividers:设置是否显示分隔线以及显示的位置。

  8. android:dividerPadding:设置分隔线的间距。

这些方法和属性可以用于灵活地控制LinearLayout布局的方向、对齐方式、权重分配等,以满足不同的布局需求。其中,方法可以通过编程方式进行设置,而属性可以在XML布局文件中进行设置。

四 LinerLayout简单案例

<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="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, LinearLayout!"
        android:textSize="24sp"
        android:layout_gravity="center_horizontal"/>
        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me!"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

上述代码创建了一个垂直方向的LinearLayout,其中包含两个子视图:一个TextView和一个Button。TextView用于显示文本内容,Button用于触发点击事件。

LinearLayout中,android:layout_widthandroid:layout_height属性分别设置为match_parent,表示填充父容器的宽度和高度。android:orientation属性设置为vertical,表示子视图按垂直方向排列。

TextView和Button的布局参数(LayoutParams)使用默认值,即wrap_content,表示根据内容自适应宽度和高度。通过android:layout_gravity属性可以调整子视图在父容器内的对齐方式。

这个简单的LinearLayout案例展示了如何在垂直方向上排列文本和按钮,并通过android:layout_gravity属性实现水平居中对齐。

五 总结

使用LinearLayout可以实现简单的线性布局,适用于需要按照水平或垂直方向对子视图进行排列的场景。它的灵活性和易用性使得开发者能够快速构建各种布局样式。


http://www.niftyadmin.cn/n/1005010.html

相关文章

【数据挖掘】时间序列教程【二】

2.4 示例&#xff1a;颗粒物浓度 在本章中&#xff0c;我们将使用美国环境保护署的一些空气污染数据作为运行样本。该数据集由 2 年和 5 年空气动力学直径小于或等于 3.2017 \&#xff08;mu\&#xff09;g/m\&#xff08;^2018\&#xff09; 的颗粒物组成。 我们将特别关注来自…

小程序UV提升策略与技术实践

引言&#xff1a; 随着智能手机的普及和移动互联网的快速发展&#xff0c;小程序成为了人们日常生活中不可或缺的一部分。对于开发者和企业来说&#xff0c;提升小程序的UV&#xff08;Unique Visitors&#xff09;即独立访客数量&#xff0c;是增加品牌曝光度和用户参与度的关…

【Python小工具】Python小工具批量提取Excel图片

目前有一个需求&#xff0c;就是批量读取当前目录下所有文件夹里的Excel文件&#xff0c;去获取出Excel文件中的图片&#xff0c;并根据图片对应的行去获取某列的值作为命名方式进行命名&#xff0c;并统一保存在一个新的文件夹里面。 自己花了几个小时写了一个小工具出来&…

万字长文,SpringSecurity实现权限系统设计

RBAC权限分析 RBAC 全称为基于角色的权限控制&#xff0c;本段将会从什么是RBAC&#xff0c;模型分类&#xff0c;什么是权限&#xff0c;用户组的使用&#xff0c;实例分析等几个方面阐述RBAC 思维导图 绘制思维导图如下 什么是RBAC RBAC 全称为用户角色权限控制&#xff…

在IDEA中使用groovy脚本生成POJO

步骤1&#xff1a;打开Database窗格&#xff0c;新建数据库连接 数据库连接默认只是当前工程使用&#xff0c;想要所有IDEA窗口共享 步骤2&#xff1a;编辑groovy脚本 步骤3&#xff1a;选择一张或多张表&#xff0c;生成代码 生成效果 附&#xff1a;groovy脚本 import com.i…

django中使用celery和接口缓存

django中使用celery celery中要使用djagno的东西&#xff0c;才要加这句话 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "luffy_api.settings.dev") 加载django的配置文件&#xff0c;&#xff0c;将app…

PyCharm 新项目关联到码云(Gitee)源代码管理

1. 在 PyCharm 中创建新项目 打开 PyCharm&#xff0c;点击 “Create New Project” 或 “File” > “New Project”&#xff0c;然后按照提示完成新项目的创建。 2. 在码云上创建新仓库 登录到你的码云账户&#xff0c;点击 “新建仓库”&#xff0c;输入仓库名称、描述…

【独家揭秘】去除、找回word文档密码!

最简单的办法找回word文档密码、去除word文档密码&#xff0c;只需几个简单的步骤就能搞定。具体步骤如下&#xff1a;第一步百度搜索“密码帝官网”&#xff0c;第二步点击“立即开始”&#xff0c;在用户中心上传需要解密的文件即可。 密码帝官网是一款安全、简单易操作的在线…