Android(java)学习笔记96:layout_weight使用注意事项
1.?android:layout_weight使用說(shuō)明:
layout_weight是權(quán)重的意思,也就是各個(gè)控件所占的比重,用在LinearLayout布局中。當(dāng)我們使用layout_weight的時(shí)候,layout_width和layout_height有三種表示方法
?
2.?android:layout_weight使用之?layout_width為0dp:
此時(shí),layout_weight使用的時(shí)候要求:layout_height = "0" ?或者 ?layout_width = "0"
比如:只有如下這樣layout_weight屬性才會(huì)生效
(1)android:layout_width = "0dip"
? ? ? ? ?android:layout_weight = "1"
(2)android:layout_height = "0dip"
? ? ? ? ?android:layout_weight = "1"
下面是一個(gè)案例:
首先看看我們的布局需求,如下:
如下布局中上下各占1/2,然后這兩個(gè)一半,分別如下方式分割為1/3
布局代碼如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmln:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:oritentation="vertical"> 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="0dip" 10 android:layout_weight="1" 11 android:oritentation="horizontal"> 12 13 <View 14 android:layout_width="0dip" 15 android:layout_height="match_parent" 16 android:background="#ff0000" 17 android:layout_weight="1" 18 /> 19 <View 20 android:layout_width="0dip" 21 android:layout_height="match_parent" 22 android:background="#0000ff" 23 android:layout_weight="1" 24 /> 25 <View 26 android:layout_width="0dip" 27 android:layout_height="match_parent" 28 android:background="#00ff00" 29 android:layout_weight="1" 30 /> 31 </LinearLayout> 32 <LinearLayout 33 android:layout_width="match_parent" 34 android:layout_height="0dip" 35 android:layout_weight="1" 36 android:oritentation="vertical"> 37 38 <View 39 android:layout_width="match_parent" 40 android:layout_height="0dip" 41 android:layout_weight="1" 42 android:background="#ff00ff" 43 /> 44 <View 45 android:layout_width="match_parent" 46 android:layout_height="0dip" 47 android:layout_weight="1" 48 android:background="#00ffff" 49 /> 50 <View 51 android:layout_width="match_parent" 52 android:layout_height="0dip" 53 android:layout_weight="1" 54 android:background="#ffff00" 55 /> 56 </LinearLayout> 57 58 </LinearLayout>
?
3.?android:layout_weight使用之 layout_width為wrap_content:
我們可以看出,首先他們是先包含自己的內(nèi)容,然后在剩下的空間中按照weight來(lái)劃分,即剩下空間按照1:2來(lái)劃分。
?
4.?android:layout_weight使用之?layout_width為match_parent:
?
我們可以看出,剛好和第一種layout_width為0dp的布局相反。
?
?
4. 以上三種情況到底是怎么回事呢?下面來(lái)看一下layout_weight的具體計(jì)算方法:
layout_weight的意思:
如果一個(gè)控件申明了這個(gè)屬性,那么這個(gè)控件的寬度(高度)等于它原有的長(zhǎng)度(寬度或高度)加上剩余空間所占有的比重。
例如:一個(gè)假設(shè)屏幕的寬度為L(zhǎng).
(1)對(duì)于第一種情況:
原有長(zhǎng)度都等于0,所以它們的長(zhǎng)度就是剩余空間所占有的比重。
第一個(gè)button所占有的比重為L(zhǎng)*1/(1+2)=1/3L;
第二個(gè)button所占有的比重為L(zhǎng)*2/(1+2)=2/3L。
?
(2)對(duì)于第三種情況:
原有長(zhǎng)度都等于match_parent即等于L,那么剩余空間的長(zhǎng)度等于L-(L+L)=-L;
對(duì)于第一個(gè)button所占有的比重為:-L*1/(1+2)=-1/3L,
對(duì)于第二個(gè)button所占有的比重為:-L*2/(1+2)=-2/3L,所以它們的總長(zhǎng)度等于原有的長(zhǎng)度加上剩余空間所占有的比重。
即L+(-1/3L)= 2/3L 和 L+(-2/3L)= 1/3L;
即反過(guò)來(lái)的2:1就是出現(xiàn)的第三種情況。
?
轉(zhuǎn)載于:https://www.cnblogs.com/hebao0514/p/4729591.html
總結(jié)
以上是生活随笔為你收集整理的Android(java)学习笔记96:layout_weight使用注意事项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 大家怎么看唐嫣成为金鹰女神?
- 下一篇: spring mvc 控制器方法传递一些