LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重。(注意:只有LinearLayout才有这个属性)

布局代码是:

<?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 几乎全部占据了所有空间!

效果如下图:

注意若将android:layout_width="match_parent",改成android:layout_width="wrap_content" ! 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 属性!

下面是来自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.)

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

android:layout_width="match_parent"

android:layout_width="wrap_content"

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

正确的用法是:先设置 android:layout_width="0dp" 或者 android:layout_height="0dp"然后再去调配权重,而此时的weight也非常好理解: weight就是比重!比例!

更多相关文章

  1. 对话框大合集
  2. Android(安卓)Material Design 系列之 TextInputLayout 使用详解
  3. Android-横屏应用在竖屏情况下解锁引起销毁
  4. (转)android中的ellipsize
  5. Android响应按钮的程序结构
  6. Android(安卓)Toast使用简介
  7. 对于android中自定义组件属性的理解
  8. Android(安卓)TableLayout 属性含义
  9. 新书内容连载(1):自定义Android组件之带图像的TextView

随机推荐

  1. Android:实现隐藏应用功能(续)
  2. Android下用Properties保存程序配置
  3. android获取组件尺寸
  4. android aidl-cpp方式接口调用出现问题
  5. Android极光推送通过不同的通知内容跳入
  6. android Toast只显示一次
  7. android中数据存储的ContentProvider的使
  8. Android(安卓)-- AndroidX库
  9. Android(安卓)文件IO操作
  10. android fragment生命周期