【聲明】轉載請註明出處,此文出自指尖飛落的博客:http://blog.csdn.net/huntersnail
——尊重作者,知識無價,交流無限!
weight是線性佈局的特有屬性,控件的寬度和高度的不同,也會存在差異,下面咱們就來小玩幾下!
水平效果
Example 1:將寬度設置爲包裹類型wrap_content/0dp
<?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:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2" />
</LinearLayout>
我們在佈局裏面設置爲線性佈局,橫向排列,然後放置兩個寬度爲0dp的按鈕,分別設置weight爲1和2,在效果圖中,我們可以看到兩個按鈕按照1:2的寬度比例正常排列了,這也是我們經常使用到的場景,這是時候很好理解,Button1的寬度就是1/(1+2)
= 1/3,Button2的寬度則是2/(1+2) = 2/3,我們可以很清楚的明白這種情景下的佔比如何計算。
Example
2:將寬度設置爲包裹類型match_parent
<?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:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2" />
</LinearLayout>
我們可以看到,在這種情況下,佔比和上面正好相反,這是怎麼回事呢?說到這裏,我們就不得不提一下weight的計算方法了。
android:layout_weight的真實含義是:如果View設置了該屬性並且有效,那麼該 View的寬度等於原有寬度(android:layout_width)加上剩餘空間的佔比。
從這個角度我們來解釋一下上面的現象。在上面的代碼中,我們設置每個Button的寬度都是match_parent,假設屏幕寬度爲L,那麼每個Button的寬度也應該都爲L,剩餘寬度就等於L-(L+L)= -L。
Button1的weight=1,剩餘寬度佔比爲1/(1+2)= 1/3,所以最終寬度爲L+1/3*(-L)=2/3L,Button2的計算類似,最終寬度爲L+2/3(-L)=1/3L。
Example
3:將高度設置爲包裹類型wrap_content/0dp
<?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:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2" />
</LinearLayout>
Example 4:將高度設置爲包裹類型match_parent
<span style="font-size:12px;"><?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:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="Button2" />
</LinearLayout></span>
舉例到此嗎,自己多試試就知道了!☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆轉載請註明出處☞指尖飛落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆