为了更好地管理Android应用的用户界面里的各组件,Android提供了布局管理器。通过使用布局管理器,Android应用图形用户界面具有良好的平台无关性。推荐使用布局管理器来管理组件的分布、大小,而不是直接设置组件的位置和大小。可以使用布局管理器嵌套布局管理器,即也可作为一个UI组件来使用。

  LinearLayout可以控制组件横向排列或者纵向排列,内容不会换行,超出屏幕部分将不会显示出来。

学习图解

LinearLayout 常用XML属性及方法

【属性一】orientation 设置子组件的排列方式(单选)

  XML: android:orientation="horizontal"

  

    horizontal:横向排列

     vertical:纵向排列

  JAVA :linearLayout.setOrientation(LinearLayout.VERTICAL); 

      LinearLayout.HORIZONTAL 横向排列

  

    LinearLayout.VERTICAL 纵向排列

  

【属性二】gravity 设置子组件的对齐方式(多选

   XML: android:gravity="center"

   

  JAVA :linearLayout.setGravity(Gravity.CENTER);

  

【属性三】baselineAligned 设置子元素基准线对弃,默认为true

  基准线:

    打开的英语练习本,那条红线就是基准线  

    

    

  XML: android:baselineAligned="false"  

  

  JAVA: linearLayout.setBaselineAligned(true);

代码:true
    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:padding="20dp"
android:text="text1"
android:textSize="30sp"> </TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:padding="10dp"
android:text="text2"
android:textSize="16sp"> </TextView>
</LinearLayout>
  效果:

  

  

【搭配属性三】baselineAlignedChildIndex LinearLayout的基准线以他的第几个子元素为准,下标从0开始

  一个LinearLayout 里面有很多 textview ,每一个 textview 都有自己的基准线,那么LinearLayout可能也是另一个LinearLayout的子元素,作为子元素 baselineAlignedChildIndex 就决定这他的一个基准线

  XML:android:baselineAlignedChildIndex="0"
  JAVA:linearLayout.setBaselineAlignedChildIndex(0);
  代码:⭐注意内部的LinearLayout,后面将在 第二个LinearLayout上添加 baselineAlignedChildIndex ,搭配  baselineAligned="false" 使用
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:text="这是text2"
android:textSize="20sp"> </TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="这是text1"
android:textSize="30sp"> </TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
android:text="这是text2"
android:textSize="15sp"> </TextView>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是text4"
android:textSize="25sp"
android:background="@android:color/holo_orange_light"
> </TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:text="text"
android:textColor="@android:color/white"
android:textSize="15sp"> </TextView>
</LinearLayout>
  效果:
  

  

  

  

  ⭐ 总结

  • 默认LinearLayout是没有基准线的,从图一和图三的对比可知。
  • 下标从0开始三个子组件,最大index为2,超过2时布局将不显示
  • 这个属性是用来决定当前LinearLayout的基准线时以哪个子组件为准的

  

Android LinearLayout线性布局详解的更多相关文章

  1. android:TableLayout表格布局详解

    http://blog.csdn.net/justoneroad/article/details/6835915 这篇博文包括的内容:1.TableLayout简介2.TableLayout行列数的确 ...

  2. [置顶] Android系统五大布局详解Layout

    我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit等 ...

  3. Android系统五大布局详解Layout

    我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit ...

  4. Android开发之线性布局详解(布局权重)

    布局权重 线性布局支持给个别的子视图设定权重,通过android:layout_weight属性.就一个视图在屏幕上占多大的空间而言,这个属性给其设 定了一个重要的值.一个大的权重值,允许它扩大到填充 ...

  5. Android 之 TableLayout 布局详解

    TableLayout简介 •简介 Tablelayout 类以行和列的形式对控件进行管理,每一行为一个 TableRow 对象,或一个 View 控件. 当为 TableRow 对象时,可在 Tab ...

  6. Android学习之基础知识六—Android四种布局详解

    一.Android基本布局 布局是一种可以放置多个控件的容器,它可以按照一定规律调整内部控件的位置,而且布局内部除了可以放置控件外,还可以放置布局,实现多层布局嵌套.布局和控件.布局和布局之间的关系如 ...

  7. Android LinearLayout线性布局

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  8. Android布局管理详解(1)—— LinearLayout 线性布局

    Android的布局方式共有6种,分别是LinearLayout(线性布局).TableLayout(表格布局).FrameLayout(帧布局).RelativeLayout(相对布局).GridL ...

  9. Android开发重点难点1:RelativeLayout(相对布局)详解

    前言 啦啦啦~博主又推出了一个新的系列啦~ 之前的Android开发系列主要以完成实验的过程为主,经常会综合许多知识来写,所以难免会有知识点的交杂,给人一种混乱的感觉. 所以博主推出“重点难点”系列, ...

随机推荐

  1. linux Init分析(原创)

    1.uboot的目标就是启动内核kernel: 2.kernel的目的就是启动应用程序,而第一个应用程序即是Init,构建根文件系统. 从uboot初始化配置后,引导内核的启动,启动函数为:start ...

  2. React Docs(1)

    安装 React在codepen上提供了一个Hello,World项目事例,只需打开网站,即可尝试React.另外还提供了一个html文件的Hello,World项目,项目中引用CDN的react.j ...

  3. Visual studio2019配置OPENCV 时属性管理器中没有Microsoft.Cpp.x64.user的解决办法

    方法一:重新下载Visual studio2017,再次打开2019就会出现Microsoft.Cpp.x64.user,感觉有些麻烦,也占电脑空间,推荐方法二. 方法二:与方法一原理相同,下载201 ...

  4. sql的练习题

    表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_i ...

  5. 蚂蚁金服开源 | 可视化图形语法G2 3.3 琢磨

    G2 是蚂蚁金服数据可视化解决方案 AntV 的一个子产品,是一套数据驱动的.高交互的可视化图形语法. 经过两个多月密锣紧鼓的开发,400+次提交,G2 3.3版本今天终于和大家见面了.自上次3.2版 ...

  6. 使用nestjs集成grpc具体操作

    两个程序中, 提供grpc服务的称为服务端, 调用grpc服务的为客户端, 以下是grpc服务端和客户端的代码编写     1. 创建两个nestjs项目demo1(端口: 3000)和demo2(端 ...

  7. vue如何新建一个项目

    第一步:安装node 首先下载安装node 安装步骤参考:https://www.cnblogs.com/qdwz/p/10820554.html window+R打开控制命令行cmd node -v ...

  8. Linux学习--4.用户和组的管理

    用户和组的管理 前言 本篇文章主要讲Linux系统下用户和组的概念,还有添加用户和组,修改用户和组的基本操作,会涉及不少与之相关的配置文件与命令的介绍,几乎所有 正文 首先,简单提下概念,用户是操作系 ...

  9. 【工具】---- webpack简析

    1. 什么是webpack 一个现代 JavaScript 应用程序的静态模块打包器(module bundler),它会分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行 ...

  10. 《ASP.NET Core 3框架揭秘》博文汇总

    在过去一段时间内,写了一系列关于ASP.NET Core 3相关的文章,其中绝大部分来源于即将出版的<ASP.NET Core 3框架揭秘>(博文只能算是"初稿",与书 ...