1.线性布局 LinearLayout

LinearLayout简单来说就是线性布局,线性肯定是具有横竖两种方向的,水平和垂直。

在使用LinearLayout的时候,需要注意以下几点

2.排列方式(orientation)

排列方式有水平和垂直两种方式

在xml文件中:

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

android:orientation="horizontal" // 水平排列

在java代码中:

linearLayout.setOrientation(LinearLayout.VERTICAL); // 设置垂直排列

linearLayout.setOrientation(LinearLayout.HORIZONTAL);// 设置水平排列

这里需要注意:

android:orientation="vertical",子View使用layout_gravity在垂直方向上的设定无效

android:orientation="horizontal",子View使用layout_gravity在水平方向上的设定无效

3.摆放位置(gravity/layout_gravity)

上面讲到了排列问题,这里在来讲一下摆放问题

gravity是针对当前控件里面内容的摆放,如果是容器,则针对的是容器里面子view的摆放;如果是控件,则针对的是控件里面内容的摆放。

layout_gravity是指当前控件在父控件里面的摆放位置,不过需要注意的一点是父控件设置的gravity的级别要低于子控件设置的layout_gravity。

我们来分析其中的一种情况然后来做个总结。

LinearLayout的排列方式为vertical,优先级最高。当LinearLayout设置了gravity,都是在LinearLayout排列方式为vertical,LinearLayout设置了gravity之后的位置重新来进行排列的。如果子view设置了layout_gravity的话,那么子View的排列方式按子View设置的为主,下面看个例子:

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linear_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:orientation="vertical">

android:layout_width="100dp"

android:layout_height="wrap_content"

android:padding="5dp"

android:text="hello"/>

android:layout_width="100dp"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:padding="5dp"

android:text="hello"/>

android:layout_width="100dp"

android:layout_height="wrap_content"

android:padding="5dp"

android:text="hello"/>

gravity_image

可以看到第二个TextView设置的layout_gravity为center_vertical,表示生效的是layout_gravity为center_vertical。所以layout_gravity相当于只设定了center_vertical(不生效)而没有设定center_horizontal,所有就出现了图中的情况。可以总结为如果父控件设置了gravity的同时,子View设置了layout_gravity,那么以子View设定的为准。

3.1 权重(layout_weight)

layout_weight

在LinearLayout布局之中,权重也是一个很重要的属性。简单来说就是按比例来分配控件占用父控件的大小。

若C-child表示子布局声明的大小,B-blank表示剩余布局的大小,P-percent表示子布局占据父布局剩余布局的比例,则子布局最终的实际大小R-reality为:

R = C + B * P

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linear_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@android:color/holo_blue_dark"

android:padding="5dp"

android:text="left"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@android:color/holo_green_dark"

android:gravity="center"

android:padding="5dp"

android:text="right"/>

weight_image

我们来按照公式来计算

R = C + B * P = 0dp + (B-0dp-0dp) * (1/2) = (1/2)B

也就是父布局的1/2。

weightSum

LinearLayout有一个权重数量的标记:weightSum。在LinearLayout中没有声明weightSum时,默认的就是各个控件权重的总和。上面的例子,默认的weightSum就是2。接下来我们再看一个例子:

这里我将weightSum设置为4。

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linear_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:weightSum="4"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@android:color/holo_blue_dark"

android:padding="5dp"

android:text="left"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@android:color/holo_green_dark"

android:gravity="center"

android:padding="5dp"

android:text="right"/>

布局的显示如下:

weight_image

0dp与wrap_content

谷歌官方建议子布局的layout_width使用0dp,来分比例显示布局,和wrap_content大同小异,当使用layout_weight时,都表示占据剩余宽度或高度的比重。

但两者有明显区别。

使用0dp时,要考虑所分配的布局宽度是否小于控件实际宽度,比如:

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linear_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@android:color/holo_blue_dark"

android:padding="5dp"

android:text="left"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="19"

android:background="@android:color/holo_green_dark"

android:gravity="center"

android:padding="5dp"

android:text="right"/>

则显示为如下:

weight_image

而使用wrap_content则不会出现上面的那种情况

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linear_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@android:color/holo_blue_dark"

android:padding="5dp"

android:text="left"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="19"

android:background="@android:color/holo_green_dark"

android:gravity="center"

android:padding="5dp"

android:text="right"/>

weight_image

这里left先获取自适应的大小,然后再去获取剩余布局的1/20。

0dp与match_parent

可以发现match_parent是与0dp的效果是相反的

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linear_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@android:color/holo_blue_dark"

android:padding="5dp"

android:text="left"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="2"

android:background="@android:color/holo_green_dark"

android:gravity="center"

android:padding="5dp"

android:text="right"/>

weight_image

这看起来很奇怪,那么我们通过公式来计算。以left为例子

B是剩余布局的大小 = 父布局大小 - 子控件大小之和

B = C - (C + C) = -C;

R = C + B * P = C + (-C) * (1/3) = 2/3C

这里的C就是父布局的大小也就是match_parent。

4.总结

LinearLayout的排列方式有垂直和水平

当LinearLayout的排列方式为vertical,也就是垂直方向时:

LinearLayout里面的子view设置layout_gravity在垂直方向上的设定是无效的。并且子view设定的layout_gravity是在前两个的基础位置上来进行摆放的。

当LinearLayout的排列方式为horizontal,也就是水平方向时:

LinearLayout里面的子view设置layout_gravity在水平方向上的设定是无效的。并且子view设定的layout_gravity是在前两个的基础位置上来进行摆放的。

权重需要记住的就是公式

控件的实际大小 = 控件设定的大小 + (布局剩余的大小) * 布局的比例

布局剩余的大小 = 父布局的大小 - 子布局大小之和

android linearlayout 方法,Android布局控件-LinearLayout详解相关推荐

  1. Android开发的之基本控件和详解四种布局方式

    Android中的控件的使用方式和iOS中控件的使用方式基本相同,都是事件驱动.给控件添加事件也有接口回调和委托代理的方式.今天这篇博客就总结一下Android中常用的基本控件以及布局方式.说到布局方 ...

  2. android控件使用大全,Android常见控件使用详解

    本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下 1.TextView 主要用于界面上显示一段文本信息 2.Button 用于和用户交互的一个按钮控件 //为Butt ...

  3. C#Winform的DataGridView控件使用详解1—七种DataGridViewColumn类型使用方法

    C#Winform的DataGridView控件使用详解1-七种DataGridViewColumn类型使用方法 DataGirdView控件Column类型 DataGridViewButtonCo ...

  4. Flash播放控件属性详解

    Flash 播放控件属性详解 一.属性篇 1.AlignMode(读写)  语法:AlignMode As Long  说明:对齐方式(与SAlign 属性联动).当控件的长宽比例与影片不一致且WMo ...

  5. C#Winform的DataGridView控件使用详解2—DataGridView表格样式设置及表格操作

    C#Winform的DataGridView控件使用详解2-DataGridView表格样式设置及表格操作 DataGridView表格样式设置 DataGridView行序号设置 右键弹出控件表格操 ...

  6. VB6.0 ActiveX 控件开发详解 [第一章:创建工程]

    前言 在CSDN的VB论坛上,我总是能够看见有人这样问"有没有这样的控件,一个列表框,每一个项前面有一个按钮"(这是例子),又或者见到这样:"怎么样做一个ActiveX控 ...

  7. QT QLabel控件(使用详解)

    本文详细的介绍了TextLabel控件的各种操作,例如:显示边框.设置文字.设置字体.设置信息提示框.状态提示.居中对齐.加载图片.自适应图片大小.设置位置大小.样式表等操作. 本文作者原创,转载请附 ...

  8. QT QTabWidget 控件 使用详解

    本文详细的介绍了QTabWidget控件的各种操作,例如:新建界面.设置页面名字.设置提示信息.设置页面激活.设置标题栏位置.设置页面关闭按钮.设置页面关闭按钮.获取页面下标.获取页面总数.清空所有页 ...

  9. QT QSpinBox 整数计数器控件 使用详解

    本文详细的介绍了QSpinBox控件的各种操作,例如:获取数值.设置前后缀.设置最大/小值.进制转换.关联信号槽.优化信号.QSS优化.文件源码.样式表 .效果:可以设置背景.边框.向上按钮.向下按钮 ...

  10. 【Android】7.1 布局控件常用的公共属性

    分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 Android应用程序中的布局控件都是容器控件,用于控制子元素的排列和放置方式.Android提供的布局控件有: ...

最新文章

  1. 抽样方法,采样方法 shuffle
  2. 人最大的荣耀不在于从未失败,而在于每次失败以后都能东山再起
  3. vue笔记整理与总结
  4. 用 Arthas 神器来诊断 HBase 异常进程
  5. Linux下jetty报java.lang.OutOfMemoryError: PermGen space及Jetty内存配置调优解决方案
  6. 数据解析学习笔记(正则解析、bs4解析、xpath解析)
  7. 问题 L: A+B Problem (IV) : Input/Output Practice 山东科技大学OJ C语言
  8. 《无码的青春》第七章 御姐
  9. mysql urlencode 中文_php url中文转码的方法
  10. mysql nb3 备份_通过Navicat进行Mysql数据库自动备份与还原
  11. 四级语法4——定语从句
  12. 读《当众讲话诀窍》-殷亚敏 (2)
  13. android rsa加密工具类,GitHub - Lerist/encrypt: Android 加密解密工具包。
  14. 搭建自己的流媒体服务器-(1)服务器搭建篇
  15. 数据库应用(mysql)数据库编程
  16. linux md5sum 的用法
  17. 需求分析中适应性怎么写_需求文档,怎么写才不会被打?
  18. 手机关机不拔电池也能被定位吗?
  19. 计算机等级考试光敏电阻,光敏电阻的基础知识介绍
  20. jetson nano opencv3.4.x安装

热门文章

  1. WPS(word)中批量设置表格根据窗口居中对齐
  2. 利用注解完成变量自动初始化
  3. c语言链表按成绩区间查询,C语言写的学生成绩管理系统(链表)
  4. python写一个爬虫(3)
  5. python刷票脚本在哪_可以挂在服务器的 12306 刷票脚本
  6. 参数化设计 | MixAI 知识库 No.58
  7. 243亿美元营收背后,百年龙头的汽车生态砝码
  8. 一辈子受用的人生格言 超励志!
  9. 基于FPGA的实现一款简易电子密码锁
  10. golang 使用gops进行程序监控