android布局詳解之layout_weight
layout_weight用于在LinearLayout中實現控件空間分配的按比例分配
我們可以使用layout_weight來實現html中的table效果
一、LinearLayout內的控件的layout_width設置為"wrap_content"
布局1:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="1"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="1"/> </LinearLayout>
效果如下:
可以看到這三個TextView是按照1:2:3的比例進行顯示的,這樣看來似乎可以實現按照比例顯示了,但是有個問題,如果TextView內的文本長度一同那么較長文本的TextView會寬度會有所增加,見下面配置及效果:
布局2:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1111111111111111111111111111111111111111111"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="3"/> </LinearLayout>
效果如下:
這樣無法實現我們所需要的按比例分配,因為設置layout_width設置為"wrap_content"。
布局3:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1111111111111111111111111111111111111111111"/> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="3"/> </LinearLayout>

二、LinearLayout內的控件的layout_width設置為"fill_parent"
布局1:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> </LinearLayout>效果如下:

奇怪吧,整個寬度平分3塊,第一個TextView占了兩塊,這樣看來weight值越小的優先級越大。只有兩個TextView似乎看出些道理,那么讓我們看看三個是什么效果:
布局2:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="3"/> </LinearLayout>
效果如下:
什么意思?第三個TextView丟掉了,很是奇怪,讓我們再試一個,把weight分別改為2,3,4的看看效果:
這個效果讓人很困惑,我一直想尋求出一個確切的比例公式,但是至今未找到。有哪位大神能搞定的話忘不吝賜教。
雖然這個android:layout_weight屬性很怪異,但幸運的是我們達到了目標:
按比例顯示LinearLayout內各個子控件,需設置android:layout_width="0dp",如果為豎直方向的設置android:layout_height="0dp"。在這種情況下某子個控件占用LinearLayout的比例為:本控件weight值 / LinearLayout內所有控件的weight值的和。
注:LinearLayout中的TextView按比例顯示的時候,layout_width="0dp"或者layout_height="0dp"
來自:http://blog.csdn.net/x359981514/article/details/8567466