前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android从零单排系列二十九】《Android布局介绍——LinerLayout》

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

作者头像
再见孙悟空_
发布2023-07-17 20:30:30
2030
发布2023-07-17 20:30:30
举报

前言

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

一 LinerLayout基本介绍

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

二 LinerLayout使用方法

在XML布局文件中定义LinearLayout:

代码语言:javascript
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
    <!-- 子视图元素 -->
    
</LinearLayout>

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

添加子视图元素: 在LinearLayout标签内部添加其他视图组件作为其子元素,例如TextView、Button等。根据需要可以使用不同的布局参数来控制子视图的大小和对齐方式。

代码语言:javascript
复制
<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作为子视图。

设置布局属性: 可以通过在每个子视图的布局参数中设置不同的属性来控制子视图在LinearLayout中的位置和大小,例如android:layout_weight属性可以用来设置子视图的权重,实现按比例分配剩余空间。

代码语言:javascript
复制
<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简单案例

代码语言:javascript
复制
<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可以实现简单的线性布局,适用于需要按照水平或垂直方向对子视图进行排列的场景。它的灵活性和易用性使得开发者能够快速构建各种布局样式。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 一 LinerLayout基本介绍
  • 二 LinerLayout使用方法
  • 三 LinerLayout常见属性及方法
  • 四 LinerLayout简单案例
  • 五 总结
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档