注意:本文原創(chuàng)豌蟋,轉(zhuǎn)載請注明出處。歡迎關(guān)注我的 簡書 桑滩。
** 本篇文章已授權(quán)微信公眾號 guolin_blog (郭霖)獨(dú)家發(fā)布*
看到本文的標(biāo)題梧疲,很多童鞋會一臉不屑的說,這有什么不了解的运准。不就是通過weight來給子布局按比例來分配空間嘛幌氮!好,這個(gè)答案也對也不對胁澳。 此時(shí)有人會疑惑了该互,為什么也對也不對? 我先來舉兩個(gè)最常見的例子:
<?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:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
</LinearLayout>
<?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:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
</LinearLayout>
這兩個(gè)例子都能實(shí)現(xiàn)上圖的效果韭畸。那么問題來了宇智,這兩個(gè)方式有什么區(qū)別嗎? 眼尖的童鞋立馬會說:當(dāng)然有區(qū)別胰丁,一個(gè) android:layout_width 是0随橘, 另一個(gè)是 wrap_content。 那么這兩個(gè)有什么區(qū)別嗎锦庸? 為什么實(shí)現(xiàn)的效果是一樣的机蔗? 我還要問一句,真的是一樣嗎?
如果對這個(gè)問題有所疑惑蜒车,那么兔子哥將帶領(lǐng)大家來深入了解一下weight和weightSum讳嘱,并通過閱讀本篇文章幔嗦,做到知其然酿愧,并且知其所以然。
什么是weight和weightSum
某位偉人曾經(jīng)說過邀泉,要對某個(gè)知識點(diǎn)深入了解嬉挡,最好的切入點(diǎn)就是仔細(xì)閱讀知識點(diǎn)的定義。至于這位偉人是誰汇恤,什么庞钢?想不起來了?不用想了因谎,就是這篇博文的作者基括,兔子哥大人也! ~~~好了财岔,扯了這么多廢話风皿!我們還是先步入正題,先看看Google是怎么給 android:layout_weight 和 android:weightSum來定義的:
Layout Weight
LinearLayout also supports assigning a weight to individual children with the android:layout_weight
attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
大體意思就是匠璧,android:layout_weight 這個(gè)屬性代表了一個(gè)“重要性”的值桐款,這個(gè)值的大小代表了該控件能在屏幕中占據(jù)多大的空間。這個(gè)值越大夷恍,表明該控件可以在父控件中占據(jù)較多的“剩余”空間魔眨。默認(rèn)的weight是0。
在這里酿雪,大家一定要注意“剩余”兩個(gè)字遏暴!大家往往容易忽略這一點(diǎn),導(dǎo)致出現(xiàn)了很多問題指黎。舉個(gè)例子:水平方向布局的父類有三個(gè)子類拓挥,父類總的寬度是100,子類的寬度分別是10袋励,20侥啤,30。 那么 android:layout_weight 這個(gè)屬性的目的茬故,就是瓜分剩余的 100 - 10 - 20 - 30盖灸,也就是剩余的40的使用權(quán)。沒錯磺芭! 就是android:layout_weight 這個(gè)屬性 僅僅決定 哪個(gè)子類能瓜分到更多的40的部分赁炎!
android:weightSum
Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children.
這個(gè)就很好理解了,weightSum定義了weight 總和的最大值。如果 android:weightSum 沒有定義徙垫,那么默認(rèn)值就是通過各個(gè)子類的 layout_weight 累加得到讥裤。
工作原理
講了這么多,舉個(gè)例子來說明一下姻报。
<?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:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/hello_world" />
</LinearLayout>
效果如下圖:
內(nèi)部的計(jì)算原理是:
Button的寬度 = Button 的 width + Button的weight ***** 父布局(LinearLayout)的寬度 / weightSum
上面的例子己英,也就是 Button的寬度 = 0 + 0.5 *** LinearLayout的寬度 / 1 = 0.5 * LinearLayout的寬度
也就是Button的寬度將占屏幕的一半。
Google官方例子
結(jié)合上面的知識再看一下 Google的例子 吴旋,可能體會的更加深刻损肛。
首先看實(shí)現(xiàn)的效果:
如果讓大家自己來實(shí)現(xiàn)這個(gè)效果的話,相信很多童鞋會采用相對布局荣瑟,將send按鈕位于屏幕最下方治拿,然后上面的三個(gè)EditText從上往下依次排列。但是message的EditText要鋪滿剩余的空間笆焰,這個(gè)怎么實(shí)現(xiàn)劫谅? 有的童鞋會不屑的說了,這有什么難的嚷掠,把message的高度設(shè)置match_parent并且位于subject和send之間即可捏检。代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<EditText
android:id="@+id/et_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/to" />
<EditText
android:id="@+id/et_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_to"
android:hint="@string/subject" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_send"
android:layout_below="@id/et_subject"
android:gravity="top"
android:hint="@string/message" />
<Button
android:id="@+id/btn_send"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:text="@string/send" />
</RelativeLayout>
但是如果采用本文講解的weight可能會更加的簡單。這個(gè)布局關(guān)鍵的一點(diǎn)是message要鋪滿subject與send之間的區(qū)域叠国。問題是如何實(shí)現(xiàn)鋪滿呢未檩? 對了! 本文講解的 weight 的核心就是瓜分父布局剩余的空間粟焊。全部瓜分不就是鋪滿嘛冤狡! 那么代碼也就出來了:
<?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:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/to" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/subject" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send" />
</LinearLayout>
著重看一下message的布局:
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message" />
沒錯,采用了極為巧妙的方法项棠。其他的子布局都不設(shè)置 layout_weight悲雳, 也就是默認(rèn)是0 。 那么message將獲取全部的剩余空間香追,實(shí)現(xiàn)了鋪滿的效果合瓢。
后記
相信閱讀到這里的童鞋們,對文章開頭的問題應(yīng)該已經(jīng)知道答案了透典。如果還有童鞋不知道答案晴楔,可以試著將文章開頭的兩個(gè)Button的內(nèi)容改一下,一個(gè)叫AAAAAAAAA峭咒,一個(gè)叫B税弃。您再看一下效果,可能就明白了凑队。至于那個(gè)也對也不對的問題则果,“不對”的原因是他們要瓜分的是 剩余 空間。本文止。