LinearLayout

•常用属性

  

•注意事项

  • 当  android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用

    • android:layout_gravity="left"
    • android:layout_gravity="right"
    • android:layout_gravity="center_horizontal"  
  • 当  android:orientation="horizontal"  时, 只有垂直方向的设置才起作用,水平方向的设置不起作用

    • 即: top , bottom , center_vertical  是生效的

Weight(转载)

•概念

Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.  

  说明:

  • 指示LinearLayout中多少额外空间分配给与这些LayoutParams关联的视图
  • 如果视图不应被拉伸,请指定0
  • 否则,额外空间将在权重大于0的所有视图中按比例分配。

  剩余布局大小(额外空间) = 父布局大小 - 子布局大小之和;

•详解

  1.额外空间,指的是剩余空闲空间, 额外空间将在权重大于0的所有视图中按比例分配。

    如下,总权重为1+1=2;

    第一个控件是比第二个控件占的空间小的,即w(12345)+1/2空闲空间< w(123456)+1/2控件;

<LinearLayout
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_height"
android:layout_weight="1"
android:text="12345"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_height"
android:layout_weight="1"
android:text="123456"/>
</LinearLayout>

    如果我们让控件的宽度定义为layout_width="0dp" ,这样比如2个控件的 layout_weight="1" 就可以各自50%平分整个空间了;

    因为:0 + 1/2空闲空间 = 0 + 1/2空闲空间。

  2.默认layout_weight为0,所以如果这么写:

<LinearLayout
android:orientation="horizontal"> <TextView
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#000" /> <Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> <TextView
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#888" /> </LinearLayout>

    则总权重为1,即Button占了所有剩余空闲空间,无论它在哪个位置。

  3.在排列方向上设置了match_parent, 如下,权重为2,1,2

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="2"/>

  运行结果如下:

    

  分析:

    • 因为设置的都是match_parent,所以如果没有设置权重,三个Button只会显示第一个,其他会被覆盖
    • 但是设置了权重后, 我们就按三个 Button 给定的 width=match_parent 计算剩余空间
    • 剩余空间=1个match_parent空间-3个match_parent空间= -2个match_parent空间(负2)
    • 所以
    • Button1所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/5 = 1/5个match_parent空间
    • Button2所占空间 = 1个match_parent空间+(-2个match_parent空间)*1/5 = 3/5个match_parent空间
    • Button3所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/5 = 1/5个match_parent空间

  

  所以在统一设置match_parent时,会有这么一个特性,权重越大,空间越小。

  而且在某个控件权重刚好为另外的所有控件权重之和时,这个控件会消失。

  如权重变为1,2,3;

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="2"/>

  运行结果如下:

       

  同样的算法:

    • Button1所占空间 = 1个match_parent空间+(-2个match_parent空间)*1/6 = 2/3个match_parent空间
    • Button2所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/6 = 1/3个match_parent空间
    • Button3所占空间 = 1个match_parent空间+(-2个match_parent空间)*3/6 = 0个match_parent空间

  本次内容转载自:Android-0.Android Studio布局中layout_weight用法  


divider(转载)

•为LinearLayout设置分割线

  很多界面开发中都会设置一些下划线,或者分割线,从而使得界面更加整洁美观,比如下面的酷狗 音乐的注册页面:

    

  对于这种线,我们通常的做法有两种:

  • 直接在布局中添加一个view,这个view的作用仅仅是显示出一条线,代码也很简单:

    • <View
      android:layout_width="match_parent"
      android:layout_height="1px"
      android:background="#000000" />
    • 这个是水平方向上的黑线,当然你也可以改成其他颜色,或者使用图片
  • 使用LinearLayout的一个divider属性
    • 1)divider : 设置作为分割线的图片
    • 2)showDividers : 设置分割线的位置

      • none(无)
      • beginning(开始)
      • end(结束)
      • middle(每两个组件间)
    • 3)dividerPadding : 设置分割线的 Padding

  本次内容转载自:LinearLayout(线性布局)

Androi Studio 之 LinearLayout的更多相关文章

  1. Androi Studio 之 RelativeLayout

    RelativeLayout简介 •基本属性 •根据父容器定位 •父容器定位属性示意图 •根据兄弟组件定位 •根据兄弟组件定位 •margin(偏移) •padding(填充) •margin与pad ...

  2. Ubuntu16.04 install android-studio-ide-162.4069837-linux

    本文讲解如何在Ununtu 16.04上安装jdk.Android Sdk.Anroid Studio.Genymotion.AndroidStudio与Genymotion绑定. 由于第一次装了双系 ...

  3. 从.NET看微软的焦虑

    节日没事,就像聊聊微软的NET. 1.孩子静悄悄,必定在作妖 截止目前,微软的市值达到1.85万亿美元,按说,这样一个宙斯级的巨无霸应该过的非常舒坦, 但是,和微软市值成鲜明的反差,我们从.NET的发 ...

  4. 将Android Studio默认布局ConstraintLayout切换成LinearLayout

    将Android Studio默认布局ConstraintLayout切换成LinearLayout     大部分人初次使用google android 扁平化布局ConstraintLayout都 ...

  5. Android Studio [水平布局LinearLayout]

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测

    目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...

  7. android studio你可能忽视的细节——启动白屏?drawable和mipmap出现的意义?这里都有!!!

    android studio用了很久了,也不知道各位小伙伴有没有还在用eclipse的,如果还有,楼主真心推荐转到android studio来吧,毕竟亲儿子,你会知道除了启动速度稍微慢些,你找不到一 ...

  8. Intellij idea 和android studio 代码给混淆

    Intellij idea 和android studio 代码给混淆 一.指令说明-optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassna ...

  9. Android APK瘦身之Android Studio Lint (代码审查)

    ******** ******** 第一部分: 瘦身内容介绍 ******** ******** 项目新版本的迭代接近尾声, 因为历史累积问题, 导致有很多无效的资源让已经臃肿的APK变得更肿, 因此 ...

随机推荐

  1. what's the print number means after called the setTimeout function in Chrome console?

    what's the print number means after called the setTimeout function in Chrome console? javascript fun ...

  2. node.js & create file

    node.js & create file node js create file if not exists https://nodejs.org/api/fs.html#fs_fs_ope ...

  3. web hack & web security

    web hack & web security https://www.hacksplaining.com/lessons https://www.hacksplaining.com/ OK ...

  4. ts 索引签名 无视多余的属性类型

    interface SquareConfig { color?: string; width?: number; [propName: string]: any; } function asd(opt ...

  5. Masterboxan INC:OPEC+达成产量协议 产油联盟内部分歧逐步加大

    本周,"OPEC+"在维也纳召开了今年首次部长级会议,并就未来两个月的石油生产计划达成协议.据了解,此次会议的协商过程可谓一波三折,虽然最后沙特意外宣布减产维持了OPEC+大体产量 ...

  6. 翻译:《实用的Python编程》02_04_Sequences

    目录| 上一节 (2.3 格式化) | 下一节 (2.5 Collections模块) 2.4 序列 序列数据类型 Python 有三种序列数据类型. 字符串:如 'Hello'.字符串是字符序列 列 ...

  7. Content type 'application/json;charset=UTF-8' not supported异常的解决过程

    首先说一下当时的场景,其实就是一个很简单的添加操作,后台传递的值是json格式的,如下图 ,后台对应的实体类, @Data @EqualsAndHashCode(callSuper = false) ...

  8. eclipse中将项目加载到tocat报错:Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules

    第一种解决方法:只需要找到导入项目的配置文件即可,举个栗子:D:\公司\iptv_gx\iptv_gx\.settings\org.eclipse.wst.common.project.facet.c ...

  9. COM技术中的VARIANT and VARIANTARG

    VARIANT and VARIANTARG Use VARIANTARG to describe arguments passed within DISPPARAMS, and VARIANT to ...

  10. Traefik-v2.x快速入门

    一.概述 traefik 与 nginx 一样,是一款优秀的反向代理工具,或者叫 Edge Router.至于使用它的原因则基于以下几点 无须重启即可更新配置 自动的服务发现与负载均衡 与 docke ...