android UI 之layout_weight 的解释及使用
2014-02-18 19:37
387 查看
转自
http://my.oschina.net/jsan/blog/191492
在Android的控件布局中,有一个奇葩的 layout_weight 属性,定义如下:
layout_weight : 用于指定剩余空闲空间的分割比例。用法:
为什么说是奇葩呢?
以上面的布局代码为例,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 是可以用来进行整个空间的分割的,如果我们让控件的宽度定义为0,这样比如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%平均分割:
下面定义了2个控件的 layout_height="0dp", layout_weight="1",实现了竖直方向50%平均分割:
layout_weight 原来是可以这么用滴
http://my.oschina.net/jsan/blog/191492
在Android的控件布局中,有一个奇葩的 layout_weight 属性,定义如下:
layout_weight : 用于指定剩余空闲空间的分割比例。用法:
01 | < LinearLayout |
02 | android:orientation = "horizontal" > |
03 |
04 | < TextView |
05 | android:layout_width = "wrap_content" |
06 | android:layout_height = "wrap_height" |
07 | android:layout_weight = "1" |
08 | android:text = "888" /> |
09 |
10 | < TextView |
11 | android:layout_width = "wrap_content" |
12 | android:layout_height = "wrap_height" |
13 | android:layout_weight = "1" |
14 | android:text = "999999" /> |
15 |
16 | </ 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 是可以用来进行整个空间的分割的,如果我们让控件的宽度定义为0,这样比如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%平均分割:
01 | < LinearLayout |
02 | android:orientation = "horizontal" > |
03 |
04 | < TextView |
05 | android:layout_width = "0dp" |
06 | android:layout_height = "wrap_height" |
07 | android:layout_weight = "1" |
08 | android:text = "888" /> |
09 |
10 | < TextView |
11 | android:layout_width = "0dp" |
12 | android:layout_height = "wrap_height" |
13 | android:layout_weight = "1" |
14 | android:text = "999999" /> |
15 |
16 | </ LinearLayout > |
01 | < LinearLayout |
02 | android:orientation = "vertical" > |
03 |
04 | < TextView |
05 | android:layout_width = "wrap_content" |
06 | android:layout_height = "0dp" |
07 | android:layout_weight = "1" |
08 | android:text = "888" /> |
09 |
10 | < TextView |
11 | android:layout_width = "wrap_content" |
12 | android:layout_height = "0dp" |
13 | android:layout_weight = "1" |
14 | android:text = "999999" /> |
15 |
16 | </ LinearLayout > |
相关文章推荐
- Android UI: LinearLayout中layout_weight 属性的使用规则
- 奇葩属性:layout_weight 的解释及使用
- 【Android UI】使用RelativeLayout与TableLayout实现登录界面
- android layout_weight的使用
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- [Android] 对android:layout_weight的一些解释
- Android实习收获:UI细节bug引发的layout_weight深入理解
- android layout_weight 使用总结
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- Android中的android:layout_weight使用详解
- 【Android UI】使用RelativeLayout与TableLayout实现登录界面
- Android中 android:layout_weight 属性 完美解释
- Android中android:layout_weight的使用
- Android按比例布局之layout_weight和weightSum的使用
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- android:layout_weight 和 android: weightSum的使用
- AndroidのUI布局之layout weight
- 奇葩属性:layout_weight 的解释及使用
- 【layout_weight权重】Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- TabHost和android:layout_height="0.0dip"以及android:layout_weight配合在布局中的使用