Android:layout_weight详解
轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/xx326664162/article/details/51721142 文章出自:薛瑄的博客
你也可以查看我的其他同類文章,也會讓你有一定的收貨!
簡介
android:layout_weight的真實(shí)含義是:一旦View設(shè)置了該屬性(假設(shè)有效的情況下),那么該 View的寬度等于原有寬度(android:layout_width)加上剩余空間的占比!
計(jì)算公式:
控件A最終寬度 = 控件A初始寬度+(屏幕寬度 - 控件寬度和)* 控件A的weight的值 /所有weight之和
下面分析中所提到的剩余寬度 = 屏幕寬度 - 控件寬度和
舉例:
上圖的六條,分表對應(yīng)下面的六段代碼,具體解析在每段代碼后。
一、
<LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_marginTop="20dp"android:layout_height="50dp"><TextViewandroid:layout_weight="1"android:layout_width="0dp"android:background="@color/blue"android:layout_height="match_parent" /><TextViewandroid:layout_weight="2"android:layout_width="0dp"android:background="@color/red"android:layout_height="match_parent" /><TextViewandroid:layout_weight="3"android:background="@color/yellow"android:layout_width="0dp"android:layout_height="match_parent" /></LinearLayout>二、
<LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_marginTop="20dp"android:layout_height="50dp"><TextViewandroid:layout_weight="1"android:layout_width="match_parent"android:background="@color/blue"android:layout_height="match_parent" /><TextViewandroid:layout_weight="2"android:layout_width="match_parent"android:background="@color/red"android:layout_height="match_parent" /><TextViewandroid:layout_weight="3"android:background="@color/yellow"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>設(shè)屏幕寬度為L,在三個(gè)view的寬度都為match_parent的情況下,原有寬度為L,三個(gè)的View的寬度都為L,剩余寬度為L-(L+L+L) = -2L
三個(gè)TextView的寬度依次為:
L+(-2L)*1/6 = (2/3)L
L+(-2L)*2/6 = (1/3)L
L+(-2L)*3/6 = (0)L
這就是為什么layout_weight設(shè)置越大顯示的占比反而越小,甚至是不顯示的原因
事實(shí)上默認(rèn)的View的weight這個(gè)值為0,一旦設(shè)置了這個(gè)值,那么所在view在繪制的時(shí)候執(zhí)行onMeasure兩次的原因就在這。
三、
<LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_marginTop="20dp"android:layout_height="50dp"><TextViewandroid:layout_weight="1"android:layout_width="100dp"android:background="@color/blue"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:layout_width="0dp"android:background="@color/red"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:background="@color/yellow"android:layout_width="0dp"android:layout_height="match_parent" /></LinearLayout>設(shè)屏幕寬度為L,剩余寬度為L-(100dp)
三個(gè)TextView的寬度依次為:
100+(L-(100dp))*1/3
0+(L-(100dp))*1/3
0+(L-(100dp))*1/3
相當(dāng)于是把L-100dp平均分為6分,然后按比重劃分給每個(gè)view
最后把100dp加到指定的view上
四、
<LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_marginTop="20dp"android:layout_height="50dp"><TextViewandroid:background="@color/black"android:layout_width="100dp"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:layout_width="0dp"android:background="@color/blue"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:layout_width="0dp"android:background="@color/red"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:background="@color/yellow"android:layout_width="0dp"android:layout_height="match_parent" /></LinearLayout>為了驗(yàn)證代碼三是正確的,所以代碼四中,加了一段寬度為100dp的黑色塊,結(jié)果三和四的顯示比例一樣
五、
<LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_marginTop="20dp"android:layout_height="50dp"><TextViewandroid:layout_weight="1"android:layout_width="wrap_content"android:background="@color/blue"android:text="123456789123456789"android:textColor="@color/white"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:layout_width="0dp"android:background="@color/red"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:background="@color/yellow"android:layout_width="0dp"android:layout_height="match_parent" /></LinearLayout>第一個(gè)view的 android:layout_width=“wrap_content”,所以初始寬度為text的長度
設(shè)屏幕寬度為L,剩余寬度為L-(text的長度)
三個(gè)TextView的寬度依次為:
text的長度+(L-(text的長度))*1/3
0+(L-(text的長度))*1/3
0+(L-(text的長度))*1/3
六、
<LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_marginTop="20dp"android:layout_height="50dp"><TextViewandroid:layout_weight="1"android:layout_width="0dp"android:background="@color/blue"android:text="123456789123456789"android:textColor="@color/white"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:layout_width="0dp"android:background="@color/red"android:layout_height="match_parent" /><TextViewandroid:layout_weight="1"android:background="@color/yellow"android:layout_width="0dp"android:layout_height="match_parent" /></LinearLayout>設(shè)屏幕寬度為L,三個(gè)view指定的android:layout_width都是0,所以剩余寬度為L
三個(gè)TextView的寬度依次為:
0+(L)*1/3
0+(L)*1/3
0+(L)*1/3
參考:
http://www.cnblogs.com/zhmore/archive/2011/11/04/2236514.html
http://m.blog.csdn.net/article/details?id=24667299
http://renyuan-1991.iteye.com/blog/2272200
總結(jié)
以上是生活随笔為你收集整理的Android:layout_weight详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ffmpeg 抽取视频原始编码数据(mp
- 下一篇: EXCEL身份证号码校验以及提取基本信息