android:layout_weight 和 android: weightSum的使用
2014-03-25 22:21
411 查看
一. 在LinearLayout 布局下使用 weight作为一个view在父View下的权重
一个View的空间根据设置的原始空间 + 按比例分割的空间 = 最后显示的空间
常用的方法:
1 . 可以这样设置每个子View得到平均的空间
每个子View设置为width="fill_parent" 或者height = "0dp" ,weight="1"
通过计算可以知道这是为什么
假设一个父View的总空间为 p
有n个子View需要均匀显示,设置为fill_parent 的话,空间也就是充满父View 即p
剩余空间 = p - n*p = (1-n)*p ;
每个分割的空间按比例来得到,均匀的话则是 1/n * p*(1-n)
然后得到每个的空间 = 基础的空间 + 分割的空间 = p + (1-n)*p * 1/n
经过化简后得到 每个的空间 等于 1/n * p = p/n ,这样就是均等显示了
2 每个子View设置为width="0dp" 或者height = "0dp" ,weight="1"
那么根据公式来计算一下,也就是
剩余空间为 p - 0 * n = p
按比例分割的空间 = 1/n * p
最后每个分配的空间 = 0 + p/n = p/n ,这样就均等显示了
3.想根据给的weight来分配空间
每个子View 的width=" 0dp" ,然后根据每个weight的需要的比例分割就是了
二 .weightSum 作为这个父View的总分割份数
如果 在父View中需要提供指定的空间给子View使用,就可以使用weightSum来指定
每个子View根据父类提供的空间分配份数
贴出代码和结果
![](http://images.cnitblog.com/i/397251/201403/252231234048740.png)
使用weightSum 分割需要的比例
weightSum = 10 ,设置了9个TextView ,每个weight = 1
weightSum = 12 ,设置了12个TextView,每个weight = 1
![](http://images.cnitblog.com/i/397251/201403/252238493422439.png)
定义了weightSum = 10
每个对应的weight
2 3 4 1
一个View的空间根据设置的原始空间 + 按比例分割的空间 = 最后显示的空间
常用的方法:
1 . 可以这样设置每个子View得到平均的空间
每个子View设置为width="fill_parent" 或者height = "0dp" ,weight="1"
通过计算可以知道这是为什么
假设一个父View的总空间为 p
有n个子View需要均匀显示,设置为fill_parent 的话,空间也就是充满父View 即p
剩余空间 = p - n*p = (1-n)*p ;
每个分割的空间按比例来得到,均匀的话则是 1/n * p*(1-n)
然后得到每个的空间 = 基础的空间 + 分割的空间 = p + (1-n)*p * 1/n
经过化简后得到 每个的空间 等于 1/n * p = p/n ,这样就是均等显示了
2 每个子View设置为width="0dp" 或者height = "0dp" ,weight="1"
那么根据公式来计算一下,也就是
剩余空间为 p - 0 * n = p
按比例分割的空间 = 1/n * p
最后每个分配的空间 = 0 + p/n = p/n ,这样就均等显示了
3.想根据给的weight来分配空间
每个子View 的width=" 0dp" ,然后根据每个weight的需要的比例分割就是了
二 .weightSum 作为这个父View的总分割份数
如果 在父View中需要提供指定的空间给子View使用,就可以使用weightSum来指定
每个子View根据父类提供的空间分配份数
贴出代码和结果
<?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="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> </LinearLayout> </LinearLayout>
![](http://images.cnitblog.com/i/397251/201403/252231234048740.png)
使用weightSum 分割需要的比例
<?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="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/backColor" android:orientation="horizontal" android:weightSum="10.0" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/backColor" android:orientation="horizontal" android:weightSum="12.0" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffa" android:text="hello 3" /> </LinearLayout> </LinearLayout>
weightSum = 10 ,设置了9个TextView ,每个weight = 1
weightSum = 12 ,设置了12个TextView,每个weight = 1
![](http://images.cnitblog.com/i/397251/201403/252238493422439.png)
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/backColor" android:orientation="horizontal" android:weightSum="10" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:background="#ffa" android:text="hello 3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> </LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/backColor" android:orientation="horizontal" android:weightSum="10" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:background="#ff0" android:text="hello 1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:background="#f00" android:text="hello 2" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:background="#ffa" android:text="hello 3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0" android:text="hello 1" /> </LinearLayout>
定义了weightSum = 10
每个对应的weight
2 3 4 1
![](http://images.cnitblog.com/i/397251/201403/252245457482757.png)
相关文章推荐
- 50个Android开发技巧(1,使用android:layout_weight和android:weightSum属性的使用)
- Android layout_weight属性使用
- Android中layout_weight的基本使用
- 使用android:layout_weight属性消除视图中的空白
- android之android:layout_weight的使用
- 在android布局中使用layout_weight的几点技巧和说明layout_weight
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- android layout_weight 属性使用方法
- TabHost和android:layout_height="0.0dip"以及android:layout_weight配合在布局中的使用
- android UI 之layout_weight 的解释及使用
- 对android LinearLayout中layout_weight属性使用
- Android Layout_weight属性完全解析以及使用ListView来实现表格
- Android(java)学习笔记153:layout_weight使用注意事项
- 实验:android:layout_weight的使用
- Android中layout_gravity与gravity及layout_weight的使用
- android layout_weight的使用
- Android LinearLayout布局嵌套及权重weight的使用
- android:layout_weight属性的简单使用
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- Android手机开发:android:layout_weight属性的使用