LinearLayout 在androidUI布局中使用非常多,它其中有个很方便又很有意思的属性 weight

,这个属性理解起来不是那么简单的,而真正理解了又觉得非常简单!

下面就通过一个例子来说明.

weight代表的含义--- android:layout_width

布局代码是:

<?xml version="1.0" encoding="utf-8"?>

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

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:layout_weight="1"

android:text="button1" />

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="5"

android:text="button2" />

</LinearLayout>

分析:

  当android:layout_width="match_parent"的时候,如果设置了weight属性,那么根据它的weight值(可以理解为优先级)来占据空间,而且这个值是越小,占的空间越大,因此此时可以理解为优先级.

比如:按钮1和按钮2的width属性都是match_parent,如果按钮1的weight= 1 按钮2的为weight = 2  那么按照优先级 按钮1先占据,按钮2后占据. 大小比例为 

按钮1 = 2/(1+2) ,按钮2 = 1/(1+2) 如下第一幅图

如果按钮1的weight我们设置为1000,按钮2的weight设置为1 那么 按钮2 几乎全部占据了所有空间!如下图第二幅

weight代表的含义--- android:layout_width

  1.  

    注意!weight的含义将发生根本行的变化!

    先看代码

    <?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >

    <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:text="button1" />

    <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_weight="1000"

    android:text="button2" />

    </LinearLayout>

 

当 android:layout_width="wrap_content"的时候,我们发现即使我们将按钮2设置成: android:layout_weight="1000"  按钮2不但没有像第一种情况下那样消失,反而占据了更多的空间,这是怎么回事儿呢?

分析: 主要的变化来自于我们设置了android:layout_width="wrap_content",也就是说一个控件在宽度上只会包裹它的内容. 如果设置上了权重,意思告诉该控件,要根据weight来尽可能的包裹内容,weight值越小,包裹越小.值越大,包裹越大. 但是再小,控件都要能包裹内容. 因此,不会像第一种情况那样消失!

正确使用weight 属性!

  1.  

    下面是来自SDK的一句话:

    In order to improve the layout efficiency when you specify the weight, you should change the width of theEditText to be zero (0dp). Setting the width to zero improves layout performance because using "wrap_content"as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

    它的大致意思就是说: 我们如果在某个方向上使用了weight ,那么我们必须在对应的方向上将width设置为0dp. 它告诉了我们设置为0dp是因为使用weight,系统是采用了另外一套计算占用空间大小的算法的.(the weight value requires another width calculation to fill the remaining space.)

  2. 2

    总结: 要正确使用weight,不要再去纠结 

    android:layout_width="match_parent"

    android:layout_width="wrap_content" 两种情况下该如何设置weight. 因为这样设置根本就是错误的用法.

    正确的用法是:

    先设置 android:layout_width="0dp" 或者 android:layout_height="0dp"

    然后再去调配权重

    而此时的weight也非常好理解: weight就是比重!比例!请看下图

Android——android weight 属性(百度)的更多相关文章

  1. Android Hack1 使用weight属性实现视图的居中显示

    本文地址:http://www.cnblogs.com/wuyudong/p/5898403.html,转载请注明源地址. 如果要实现如下图所示的将按钮居中显示,并且占据父视图的一半,无论屏幕是否旋转 ...

  2. Android weight属性详解

    android:layout_weight是一个经常会用到的属性,它只在LinearLayout中生效,下面我们就来看一下: 当我们把组件宽度设置都为”match_parent”时: <Butt ...

  3. android gravity属性 和 weight属性

    来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  4. 【转】android gravity属性 和 weight属性

    有点忘记这两个属性了,复习一下. 来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <Linea ...

  5. Android开发技巧一--weight属性实现视图的居中(半)显示

    面试时,一位面试官问到:“如果我想讲按钮居中显示,并且占据其父视图宽度的一半,应该怎么做到呢?”即实现这种效果: 我们使用weightSum属性和layout_weight属性实现这一要求: < ...

  6. Android之使用weight属性实现控件的按比例分配空间

    从今天開始,把看书时候的知识点整理成博客, 这个比較简单,预计有经验的都用过,weight属性 在做Android布局的时候,常常遇到须要几个控件按比例分配空间的情况 比方下图效果 在底部设置两个bu ...

  7. android: android 布局中的weight 属性

    android: weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异. 示例1:将宽度设置为包裹类型wrap_content或0dp <?xml version=" ...

  8. Android控件属性大全(转)

    http://blog.csdn.net/pku_android/article/details/7365685 LinearLayout         线性布局        子元素任意: Tab ...

  9. Android:weight,margin,padding详解实例

    weight详解 weight是用来等比例划分区域的属性. 案例代码 <LinearLayout xmlns:android="http://schemas.android.com/a ...

随机推荐

  1. Vue组件开发实践之scopedSlot的传递

    收录待用,修改转载已取得腾讯云授权 导语 现今的前端开发都讲究模块化组件化,即把公共的交互和功能封装到一个个的组件之中,在开发整体界面的时候就能像搭积木一样快速清晰高效.在使用Vue开发我们的vhtm ...

  2. 隐马尔科夫模型(Hidden Markov Models)

    链接汇总 http://www.csie.ntnu.edu.tw/~u91029/HiddenMarkovModel.html 演算法笔记 http://read.pudn.com/downloads ...

  3. windows获取本机MAC地址并写入文件的bat

    windows获取本机MAC地址并写入文件的bat MAC(Media Access Control)地址,或称为 MAC地址.硬件地址,用来定义网络设备的位置. bat代码例如以下: @echo o ...

  4. 如何获取浏览器URL中查询字符串的参数?

    如何获取浏览器URL中查询字符串的参数? 想要知道怎样解决这个问题,首先我们先认识一下Location对象. Location对象包含了当前页面与位置(url)相关的信息 URL示例:http://w ...

  5. mysql基础知识之-数据库的创建、查看等常用操作

    命令创建mysql数据库: 先启动mysql数据库,连接数据库: mysql -uroot -p123456         (语法:mysql -u登录名 -p密码) 创建表: create dat ...

  6. SQL语言基本操作(聚合函数)

    一.聚合函数 1.标量函数:只能对单个的数字或值进行计算.主要包括字符函数.日期/时间函数.数值函数和转换函数这四类.如LEFT/RIGHT/SUBSTRING/LTRIM/RTRIM/CONCAT/ ...

  7. Cg入门21:Fragment shader - 2D纹理採样

    体纹理:是啥? tex2D 曾经仅仅能在Fragment程序中纹理採样 UV坐标系:事实上点为左下角,范围为[0,1].U为x轴,V为y轴 watermark/2/text/aHR0cDovL2Jsb ...

  8. Hbase总结(八)Hbase中的Coprocessor

    1.起因(Why HBase  Coprocessor) HBase作为列族数据库最常常被人诟病的特性包含:无法轻易建立"二级索引",难以运行求和.计数.排序等操作.比方,在旧版本 ...

  9. OPENERP 构建动态视图

    来自:http://shine-it.net/index.php/topic,16142.0.html 在openerp展示界面通常是通过定义class的view(xml文件)来实现的. 有时这种方法 ...

  10. 使用RMAN方式清除

    使用RMAN方式清除 RMAN清除方式会自动清除磁盘上的归档日志文件,同时会释放控制文件中对应的归档日志的归档信息. 可以基于不同的条件来清除归档日志,如基于SCN,基于SEQUENCE,基于TIME ...