layout_weight 的解释及使用

转自:http://my.oschina.net/jsan/blog/191492

在Android的控件布局中,有一个奇葩的 layout_weight 属性,定义如下:

layout_weight : 用于指定剩余空闲空间的分割比例。用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<LinearLayout
  android:orientation="horizontal">
 
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_height"
      android:layout_weight="1"
      android:text="888"/>
 
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_height"
      android:layout_weight="1"
      android:text="999999"/>
 
</LinearLayout>

为什么说是奇葩呢?

以上面的布局代码为例,TextView-888 和 TextView-999999 是横向排列的2个控件,它们的layout_weight="1",说明这2个控件平分了所在LinearLayout的剩余的空闲空间, 我们很容易的就误认为这2个控件平分了水平方向的空间,即:各自占据了 50% 的宽度。

其实这是错误的,而是:TextView-999999控件所占据的宽度 > TextView-888所占据的宽度。因为999999字符占据的宽度大于888占据的宽度,即:w(999999) + 1/2空闲空间 > w(888) + 1/2空闲空间

这就是它奇葩的地方,很容易就让我们一直误认为是整个控件分割空间。到这里,大家一定会认为,这样的话,layout_weight 这个属性就没有什么意义了,原以为它可以分配空间呢,原来只是分割剩余空闲空间

其实,呵呵,layout_weight 是可以用来,这样比如2个控件的 layout_weight="1" 就可以各自50%平分整个空间了,因为:0 + 1/2空闲空间 = 0 + 1/2空闲空间

这是一个小技巧,也是非常实用的一个实用layout_weight分割方案:定义控件的 layout_width="0dp" 或 layout_height="0dp" 配上 layout_weight 就可以实现对整个空间的比例分割了。

下面定义了2个控件的 layout_width="0dp", layout_weight="1",实现了水平方向50%平均分割:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<LinearLayout
  android:orientation="horizontal">
 
  <TextView
      android:layout_width="0dp"
      android:layout_height="wrap_height"
      android:layout_weight="1"
      android:text="888"/>
 
  <TextView
      android:layout_width="0dp"
      android:layout_height="wrap_height"
      android:layout_weight="1"
      android:text="999999"/>
 
</LinearLayout>

下面定义了2个控件的 layout_height="0dp", layout_weight="1",实现了竖直方向50%平均分割:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<LinearLayout
  android:orientation="vertical">
 
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:text="888"/>
 
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:text="999999"/>
 
</LinearLayout>

 

使用Layout_weight时空间分配的计算方法:http://mobile.51cto.com/abased-375428.htm

layout_weight 的解释及使用的更多相关文章

  1. 奇葩属性:layout_weight 的解释及使用

    在Android的控件布局中,有一个奇葩的 layout_weight 属性,定义如下: layout_weight : 用于指定剩余空闲空间的分割比例.用法: 01 <LinearLayout ...

  2. [Android学习笔记]LinearLayout布局,剩余空间的使用

    转自:http://segmentfault.com/q/1010000000095725 如果使得一个View占用其父View的剩余空间? 答案是使用:android:layout_weight = ...

  3. Android layout_weight的用法

    android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重. 下面通过举例说明: <LinearLayout xmlns:android=& ...

  4. android:layout_weight越大所占比例越大和越大所占比例越小的两个例子

    摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...

  5. Android_Fragment_Fragment具体解释

    Android_Fragment_Fragment具体解释 分类: Android基础2013-10-03 08:23 92人阅读 评论(0) 收藏 举报 AndroidFragmentFragmen ...

  6. Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)

     [Android布局学习系列]   1.Android 布局学习之--Layout(布局)具体解释一   2.Android 布局学习之--Layout(布局)具体解释二(常见布局和布局參数)   ...

  7. Android基础笔记(十)- 帧动画、补间动画具体解释、对话框

    帧动画 补间动画Tween Animation 对话框以及面试中的注意点 帧动画 帧动画非常easy,我们首先看一下Google官方解释This is a traditional animation ...

  8. android:layout_weight属性的使用方法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...

  9. 【转】安卓布局:layout_weight的理解

    android:layout_weight详细分析介绍: 布局文件是:<?xml version="1.0" encoding="utf-8"?>& ...

随机推荐

  1. 变态最大值--nyoj题目811

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...

  2. VirtualBox 上安装CentOS 6.5

    目标:1.在VirtualBox中安装CentOS2.配置虚拟机网络,实现:    a.主机联网后,宿机能够通过主机上网    b.不管主机联网与否,主机都能SSH登录宿机,并且主宿机能互相传送文件 ...

  3. struts2_20140720

    有这样的感觉:前面学的东西弄会了,过了一段时间又感觉陌生了,还要重新开始.这次想个好办法,把写的程序用博客记录下来,把自己的学历历程用博客的形式呈现出来,一来可以方便复习,而来可以以后开发程序可以快速 ...

  4. 一封推荐信——android培训机构

    我,男,23岁,即将毕业的大四学生,就读于天津一所二本院校,计算机科学与技术专业.大一期间,进入新校园,和同学到各个宿舍推销陶瓷杯,国美电器饮水机促销员,组团蹬车游市区,不断地去探索.尝试,追求内心向 ...

  5. [转]ArcGIS移动客户端离线地图的几种解决方案

    原文地址:http://blog.chinaunix.net/uid-10914615-id-3023158.html 移动GIS中,通常将数据分为两大类:basemap layer和operatio ...

  6. yum cannot retrieve metalink for repository

    Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

  7. redhat5安装jdk6、eclipse和tomcat6

    redhat5安装jdk6.eclipse和tomcat6 1.安装jdk6 首先下载jdk(jdk-6u13-linux-i586.bin),对于redhat5,本人强烈建议不要安装jdk7,因为有 ...

  8. jQuery中设置form表单中action值的方法

    jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈   html代码: <form id="myFormId&quo ...

  9. 移动前端开发之viewport的深入理解(转)

    在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或 ...

  10. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...