最近在搞UI部分,對佈局有了一定的認識(僅限於各人的理解)歡迎來拍磚。
首先對android:layout_height="0.0dip"的屬性很是迷惑,在網上找了一大堆的東西,其實也並沒有說出來一個所以然來。找了很多文章發現一個規律就是:當android:layout_height="0.0dip"時一般都是和android:layout_weight結合來使用的,來控制子控件在父控件中所佔用的比例(水平和豎直方向:水平方向是android:layout_weight和android:layout_width="0.0dip"相互結合使用的;垂直方向是android:layout_weight和android:layout_height="0.0dip"相互結合使用的;此時android:layout_weight的值越大,佔的比例就越大。這時的“0.0dip”其實也不能用“wrap_content”來替代的(我原來以爲可以),此時子控件比例會發生小的變化。如果有3個以上的子控件用了上面的方案,假設是豎直方向成比例,如果此時把中間的一個子控件的android:layout_height="0.0dip"改爲“fill_parent”,那麼原有的比例設置就會徹底失效,一眼就能看出來。如果全部改爲“fill_parent”,那麼android:layout_width的值越小佔的比例就越大。所以,一句話總結:要想使子控件成比例,在豎直方向:用android:layout_height="0.0dip"和android:layout_weight;在水平方向:用用android:layout_width="0.0dip"和android:layout_weight;此時android:layout_weight越大在相應方向佔的比例就越大)。下面來結合代碼和圖片來說明。
1、在豎直方向有4個TextView,分別是紅、黑、白、藍四種顏色,所佔比例一次是:1:2:3:4,子控件高度全是“0.0dip” ,佈局文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/sample_1" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1" android:textSize="@dimen/text_size_big" android:background="@color/red" android:text="@string/text1" android:gravity="center" /> <TextView android:id="@+id/sample_2" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="2" android:textSize="@dimen/text_size_big" android:background="@color/black" android:text="@string/text2" android:gravity="center" /> <TextView android:id="@+id/sample_3" android:layout_width="fill_parent" android:layout_height="0.0dip" android:textSize="@dimen/text_size_big" android:layout_weight="3" android:background="@color/white" android:text="@string/text3" android:gravity="center" /> <TextView android:id="@+id/sample_4" android:layout_width="fill_parent" android:layout_height="0.0dip" android:textSize="@dimen/text_size_big" android:layout_weight="4" android:background="@color/blue" android:text="@string/text4" android:gravity="center" /> </LinearLayout>
效果圖如下:
效果是1:2:3:4
2、如果把4個TextView中的兩個的高度由“0.dip”改爲“wrap_content”,代碼和效果如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/sample_1" 9 android:layout_width="fill_parent" 10 android:layout_height="0.0dip" 11 android:layout_weight="1" 12 android:textSize="@dimen/text_size_big" 13 android:background="@color/red" 14 android:text="@string/text1" 15 android:gravity="center" 16 /> 17 18 <TextView 19 android:id="@+id/sample_2" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:layout_weight="2" 23 android:textSize="@dimen/text_size_big" 24 android:background="@color/black" 25 android:text="@string/text2" 26 android:gravity="center" 27 /> 28 29 <TextView 30 android:id="@+id/sample_3" 31 android:layout_width="fill_parent" 32 android:layout_height="wrap_content" 33 android:textSize="@dimen/text_size_big" 34 android:layout_weight="3" 35 android:background="@color/white" 36 android:text="@string/text3" 37 android:gravity="center" 38 /> 39 40 <TextView 41 android:id="@+id/sample_4" 42 android:layout_width="fill_parent" 43 android:layout_height="0.0dip" 44 android:textSize="@dimen/text_size_big" 45 android:layout_weight="4" 46 android:background="@color/blue" 47 android:text="@string/text4" 48 android:gravity="center" /> 49 50 </LinearLayout>
效果圖:
效果不是1:2:3:4,第一個明顯小很多;
全部改爲“wrap_content”後的代碼和效果圖:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/sample_1" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:layout_weight="1" 12 android:textSize="@dimen/text_size_big" 13 android:background="@color/red" 14 android:text="@string/text1" 15 android:gravity="center" 16 /> 17 18 <TextView 19 android:id="@+id/sample_2" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:layout_weight="2" 23 android:textSize="@dimen/text_size_big" 24 android:background="@color/black" 25 android:text="@string/text2" 26 android:gravity="center" 27 /> 28 29 <TextView 30 android:id="@+id/sample_3" 31 android:layout_width="fill_parent" 32 android:layout_height="wrap_content" 33 android:textSize="@dimen/text_size_big" 34 android:layout_weight="3" 35 android:background="@color/white" 36 android:text="@string/text3" 37 android:gravity="center" 38 /> 39 40 <TextView 41 android:id="@+id/sample_4" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:textSize="@dimen/text_size_big" 45 android:layout_weight="4" 46 android:background="@color/blue" 47 android:text="@string/text4" 48 android:gravity="center" /> 49 50 </LinearLayout>
效果圖:
全部改爲“wrap_content”後,比例並不是原來的1:2:3:4了,從上圖就可以看出來;
全部是“0.0dip”和其中兩個TextView是"wrap_content"的圖片的對比:
兩個是“wrap_content”和全不是“wrap_content”的對比:
如果把第二個TextView就是黑色的那個的高度改爲改爲“fill_parent”,其它的還是"wrap_content"效果如下:
當黑色的改爲“fill_parent”後, 因爲第一個紅色wrap_content,先給它分配,然後第二個黑色會把剩餘空間的全部佔用,就顯示不出來3和4了;
第三個就是白色那個改爲fill_parent其他都是wrap_content
同理,如果白色爲“fill_parent”,會先給1和2就是紅色和黑色分配,然後剩餘的空間白色會全部佔用;
第3個也就是白色的TextView高度改爲"fill_parent",其它的TextView高度改爲“0.0dip”效果如下:
這個時候,屏幕就全爲白色的了。
via:http://www.cnblogs.com/zb-xxzjjcbx/archive/2013/01/05/2845641.html