首先来查看android sdk文档,有这么一段话

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space is should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

我们从文档中可以看出LinearLayout中子控件可以使用layout_weight 属性来分配所占的“权重”,但是实际使用的过程中,经常会出错,会让人感到疑惑,下面是自己总结的一些经验:

以水平(垂直方向类似)LinearLayout为例:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" >
<Button
android:id="@+id/crime_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/crime_solved_label" />
</LinearLayout>

LinearLayout是个水平线性布局,会根据每个子成员layout_width和layout_weight来决定应该给他们分配多少空间, 遵循以下规则:

  1. 首先根据layout_width的值来初步指定空间,因为layout_width都是wrap_content, 那布局管理器会分别给两个子控件足够的空间来用于水平方向的拉伸,结果就是下面图片:

  2. 然后,因为水平方向上仍然有足够的空间,那么布局管理器就会将这个多余的控件按照layout_weight的值进行分配。上面的例子中是1:1,那么会将多余控件按照1:1的比例分配,那效果就变成了:
    •   
  3. 如果想给两个控件设置为宽度相同,Android推荐的做法是将两个控件的属性这么设置:layout_width=0dp, layout_weight=1
  4. 如果一个控件没提供layout_weight,那么android会默认其为0
  5. 由于不同的程序员有不同的写代码的惯例,android允许layout_weight的值是小数或整数,分配比例就是各自(layout_weight值)/(各自的layout_weight相加的值),这条规则成立的前提是LinearLayout没有设置android:weightSum来限制总和。

来再看一个经常让人误解的例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:text="small"
android:textColor="#02F"
android:layout_width="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_height="wrap_content"
android:textColor="#F20"
android:text="A very very long text that needs to wrap.A very very long"
android:layout_width="wrap_content"
android:layout_weight="1" />
</LinearLayout>

它的效果是这样:

它并没有按照weight去1:1的分配空间比例,这是因为布局管理器按照第一条规则满足了layout_width=’wrap_content’,这种情况下,如果确实想按照1:1的空间来分配,就只能设置layout_width=’0dp”, 这样做的效果就是这样:

欢迎斧正

Android UI: LinearLayout中layout_weight 属性的使用规则的更多相关文章

  1. shape和selector是Android UI设计中经常用到的

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

  2. Appium 在 Android UI 测试中的应用

    原文地址:https://blog.coding.net/blog/Appium-Android-UI Android 测试工具与 Appium 简介 Appium 是一个 C/S 架构的,支持 An ...

  3. background,position,绝对定位中位置属性的定位规则,cursor

    backgorund背景 background-color:red; 背景颜色 background-image:url(路径);背景图片 background-repeat:no-repeat;不重 ...

  4. Android的LinearLayout中的权重android:layout_weight

    当前EditText和Button部件只是适应了他们各自内容的大小,如下图所示: 这样设置对按钮来说很合适,但是对于文本框来说就不太好了,因为用户可能输入更长的文本内容.因此如果能够占满整个屏幕宽度会 ...

  5. Android 布局学习之——LinearLayout的layout_weight属性

    一直对layout_weight属性感到比较困惑,今天学习一下,来深入了解layout_weight属性和它的用法.     定义     首先,看看Android官方文档是怎么说的,毕竟人家才是权威 ...

  6. android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果

    主类:IndexAnimationLinearLayout.java package com.yw.sortlistview; import java.util.ArrayList; import j ...

  7. Android的LinearLayout中orientation默认值为什么是HORIZONTAL

    在一个偶然(闲着无聊)的过程中,突然非常好奇为什么LinearLayout在不指定排列方向为垂直(VERTICAL)时就得是水平方向(HORIZONTAL)排列的.产生这个疑问的时候脑子里蹦出来的第一 ...

  8. Android LinearLayout中weight属性的意义与使用方式

    layout_weight 分割父级容器的比例

  9. Android 自定义view中的属性,命名空间,以及tools标签

    昨日看到有人在知乎上问这3个琐碎的小知识点,今天索性就整理了一下,其实这些知识点并不难,但是很多开发者平时很少注意到这些, 导致的后果就是开发的时候 经常会被ide报错,开发效率很低,或者看开源代码的 ...

随机推荐

  1. Android HandlerThread源码解析

    在上一章Handler源码解析文章中,我们知道App的主线程通过Handler机制完成了一个线程的消息循环.那么我们自己也可以新建一个线程,在线程里面创建一个Looper,完成消息循环,可以做一些定时 ...

  2. python __builtins__ copyright类 (14)

    14.'copyright', 版权 class _Printer(builtins.object) | interactive prompt objects for printing the lic ...

  3. bzoj 1874: [BeiJing2009 WinterCamp]取石子游戏【博弈论】

    先预处理出来sg值,然后先手必败状态就是sg[a[i]]的xor和为0(nim) 如果xor和不为0,那么一定有办法通过一步让xor和为0,具体就是选一个最大的sg[a[i]],把它去成其他sg值的x ...

  4. 分析spring aop的源码实现

    AOP就是面向切面编程,我们可以从几个层面来实现AOP. 在编译器修改源代码,在运行期字节码加载前修改字节码或字节码加载后动态创建代理类的字节码,以下是各种实现机制的比较. spring AOP是Sp ...

  5. 编译boost asio http/server 方法

    这段时间学习boost 的asio 编程,想编译asio自带的http/server的程序,无奈在网上根本找不到方法,只能自己摸索学习. 登陆boost asio 的example 目录,(我 boo ...

  6. Luogu P1850换教室【期望dp】By cellur925

    题目传送门 首先这个题我们一看它就是和概率期望有关,而大多数时候在OI中遇到他们时,都是与dp相关的. \(Vergil\)学长表示,作为\(NOIp2016\)的当事人,他们考前奶联赛一定不会考概率 ...

  7. 最小公倍数的最小和(Minimum Sum LCM )

    #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> u ...

  8. BZOJ5484(LIS性质+树状数组)

    题目传送 学习的这篇题解. 结论: 1.直观感受一下会发现找到LIS,LIS里的东西相对位置是不会变的,其他的移一移总会排序成功的,所以其他的就是最小集合了,第一问的答案就是n-LIS: 2.寻找字典 ...

  9. [BZOJ3916/WOJ3815]Friends

    题目链接: 传送门 题目: Description 有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到U.现在你得到了U,请你找 ...

  10. Appium + python自动化 - 启动app

    各种百度将Appium + python的环境搭建好后,开启移动app自动化的探索(基于Android),首先来记录下如何启动待测的app吧! 如何启动app呢?首先要获取包名,然后获取launche ...