您的位置:首页 > 移动开发 > Android开发

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根据父类提供的空间分配份数

贴出代码和结果

<?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>




使用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



<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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: