android:layout_weight 和 android: weightSum的使用

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

贴出代码和结果

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:orientation="horizontal" >
11 
12         <TextView
13             android:layout_width="0dp"
14             android:layout_height="wrap_content"
15             android:layout_weight="1"
16             android:background="#ff0"
17             android:text="hello 1" />
18 
19         <TextView
20             android:layout_width="0dp"
21             android:layout_height="wrap_content"
22             android:layout_weight="1"
23             android:background="#f00"
24             android:text="hello 2" />
25 
26 
27         <TextView
28             android:layout_width="0dp"
29             android:layout_height="wrap_content"
30             android:layout_weight="1"
31             android:background="#ffa"
32             android:text="hello 3" />
33     </LinearLayout>
34 
35 </LinearLayout>

使用weightSum 分割需要的比例

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:orientation="vertical" >
  6 
  7     <LinearLayout
  8         android:layout_width="match_parent"
  9         android:layout_height="wrap_content"
 10         android:layout_marginTop="20dp"
 11         android:background="@color/backColor"
 12         android:orientation="horizontal"
 13         android:weightSum="10.0" >
 14 
 15         <TextView
 16             android:layout_width="0dp"
 17             android:layout_height="wrap_content"
 18             android:layout_weight="1"
 19             android:background="#ff0"
 20             android:text="hello 1" />
 21 
 22         <TextView
 23             android:layout_width="0dp"
 24             android:layout_height="wrap_content"
 25             android:layout_weight="1"
 26             android:background="#f00"
 27             android:text="hello 2" />
 28 
 29         <TextView
 30             android:layout_width="0dp"
 31             android:layout_height="wrap_content"
 32             android:layout_weight="1"
 33             android:background="#ffa"
 34             android:text="hello 3" />
 35 
 36         <TextView
 37             android:layout_width="0dp"
 38             android:layout_height="wrap_content"
 39             android:layout_weight="1"
 40             android:background="#ff0"
 41             android:text="hello 1" />
 42 
 43         <TextView
 44             android:layout_width="0dp"
 45             android:layout_height="wrap_content"
 46             android:layout_weight="1"
 47             android:background="#f00"
 48             android:text="hello 2" />
 49 
 50         <TextView
 51             android:layout_width="0dp"
 52             android:layout_height="wrap_content"
 53             android:layout_weight="1"
 54             android:background="#ffa"
 55             android:text="hello 3" />
 56 
 57         <TextView
 58             android:layout_width="0dp"
 59             android:layout_height="wrap_content"
 60             android:layout_weight="1"
 61             android:background="#ff0"
 62             android:text="hello 1" />
 63 
 64         <TextView
 65             android:layout_width="0dp"
 66             android:layout_height="wrap_content"
 67             android:layout_weight="1"
 68             android:background="#f00"
 69             android:text="hello 2" />
 70 
 71         <TextView
 72             android:layout_width="0dp"
 73             android:layout_height="wrap_content"
 74             android:layout_weight="1"
 75             android:background="#ffa"
 76             android:text="hello 3" />
 77     </LinearLayout>
 78 
 79     <LinearLayout
 80         android:layout_width="match_parent"
 81         android:layout_height="wrap_content"
 82         android:layout_marginTop="20dp"
 83         android:background="@color/backColor"
 84         android:orientation="horizontal"
 85         android:weightSum="12.0" >
 86 
 87         <TextView
 88             android:layout_width="0dp"
 89             android:layout_height="wrap_content"
 90             android:layout_weight="1"
 91             android:background="#ff0"
 92             android:text="hello 1" />
 93 
 94         <TextView
 95             android:layout_width="0dp"
 96             android:layout_height="wrap_content"
 97             android:layout_weight="1"
 98             android:background="#f00"
 99             android:text="hello 2" />
100 
101         <TextView
102             android:layout_width="0dp"
103             android:layout_height="wrap_content"
104             android:layout_weight="1"
105             android:background="#ffa"
106             android:text="hello 3" />
107 
108         <TextView
109             android:layout_width="0dp"
110             android:layout_height="wrap_content"
111             android:layout_weight="1"
112             android:background="#ff0"
113             android:text="hello 1" />
114 
115         <TextView
116             android:layout_width="0dp"
117             android:layout_height="wrap_content"
118             android:layout_weight="1"
119             android:background="#f00"
120             android:text="hello 2" />
121 
122         <TextView
123             android:layout_width="0dp"
124             android:layout_height="wrap_content"
125             android:layout_weight="1"
126             android:background="#ffa"
127             android:text="hello 3" />
128 
129         <TextView
130             android:layout_width="0dp"
131             android:layout_height="wrap_content"
132             android:layout_weight="1"
133             android:background="#ff0"
134             android:text="hello 1" />
135 
136         <TextView
137             android:layout_width="0dp"
138             android:layout_height="wrap_content"
139             android:layout_weight="1"
140             android:background="#f00"
141             android:text="hello 2" />
142 
143         <TextView
144             android:layout_width="0dp"
145             android:layout_height="wrap_content"
146             android:layout_weight="1"
147             android:background="#ffa"
148             android:text="hello 3" />
149 
150         <TextView
151             android:layout_width="0dp"
152             android:layout_height="wrap_content"
153             android:layout_weight="1"
154             android:background="#ff0"
155             android:text="hello 1" />
156 
157         <TextView
158             android:layout_width="0dp"
159             android:layout_height="wrap_content"
160             android:layout_weight="1"
161             android:background="#f00"
162             android:text="hello 2" />
163 
164         <TextView
165             android:layout_width="0dp"
166             android:layout_height="wrap_content"
167             android:layout_weight="1"
168             android:background="#ffa"
169             android:text="hello 3" />
170     </LinearLayout>
171 
172 </LinearLayout>

weightSum = 10 ,设置了9个TextView ,每个weight = 1

weightSum = 12 ,设置了12个TextView,每个weight = 1

 1 <LinearLayout
 2         android:layout_width="match_parent"
 3         android:layout_height="wrap_content"
 4         android:layout_marginTop="20dp"
 5         android:background="@color/backColor"
 6         android:orientation="horizontal"
 7         android:weightSum="10" >
 8 
 9         <TextView
10             android:layout_width="0dp"
11             android:layout_height="wrap_content"
12             android:layout_weight="2"
13             android:background="#ff0"
14             android:text="hello 1" />
15 
16         <TextView
17             android:layout_width="0dp"
18             android:layout_height="wrap_content"
19             android:layout_weight="3"
20             android:background="#f00"
21             android:text="hello 2" />
22 
23         <TextView
24             android:layout_width="0dp"
25             android:layout_height="wrap_content"
26             android:layout_weight="4"
27             android:background="#ffa"
28             android:text="hello 3" />
29 
30         <TextView
31             android:layout_width="0dp"
32             android:layout_height="wrap_content"
33             android:layout_weight="1"
34             android:background="#ff0"
35             android:text="hello 1" />
36     </LinearLayout><LinearLayout
37         android:layout_width="match_parent"
38         android:layout_height="wrap_content"
39         android:layout_marginTop="20dp"
40         android:background="@color/backColor"
41         android:orientation="horizontal"
42         android:weightSum="10" >
43 
44         <TextView
45             android:layout_width="0dp"
46             android:layout_height="wrap_content"
47             android:layout_weight="2"
48             android:background="#ff0"
49             android:text="hello 1" />
50 
51         <TextView
52             android:layout_width="0dp"
53             android:layout_height="wrap_content"
54             android:layout_weight="3"
55             android:background="#f00"
56             android:text="hello 2" />
57 
58         <TextView
59             android:layout_width="0dp"
60             android:layout_height="wrap_content"
61             android:layout_weight="4"
62             android:background="#ffa"
63             android:text="hello 3" />
64 
65         <TextView
66             android:layout_width="0dp"
67             android:layout_height="wrap_content"
68             android:layout_weight="1"
69             android:background="#ff0"
70             android:text="hello 1" />
71     </LinearLayout>

定义了weightSum = 10 

每个对应的weight

 2           3            4        1

原文地址:https://www.cnblogs.com/act262/p/3624359.html