日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Android:layout_weight详解

發(fā)布時間:2023/12/16 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android:layout_weight详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(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,在三個view的寬度都為match_parent的情況下,原有寬度為L,三個的View的寬度都為L,剩余寬度為L-(L+L+L) = -2L

三個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這個值為0,一旦設(shè)置了這個值,那么所在view在繪制的時候執(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)

三個TextView的寬度依次為:
100+(L-(100dp))*1/3
0+(L-(100dp))*1/3
0+(L-(100dp))*1/3

相當(dāng)于是把L-100dp平均分為6分,然后按比重劃分給每個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>

第一個view的 android:layout_width=“wrap_content”,所以初始寬度為text的長度

設(shè)屏幕寬度為L,剩余寬度為L-(text的長度)

三個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,三個view指定的android:layout_width都是0,所以剩余寬度為L

三個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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。