當控制項本身layout_width設定為fill_parent的時候,layout_weight數值越小,所佔空間越大,但儘可能大是有限度的,即fill_parent.
當控制項本身layout_width設定為wrap_content的時候,layout_weight數值越小,所佔空間也越小,但這個小是有限度的,即wrap_content.
例子
看了一下源碼,雖說不太懂,但瞭解了下大概意思,按照自己的理解總結一下,直接寫一下簡化的代碼吧(下面的代碼是LinearLayout源檔案中一部分的精簡,變數名稱含義可能不準確,為敘述方便暫作此解釋):
| 代碼如下 |
複製代碼 |
//Either expand children with weight to take up available space or // shrink them if they extend beyond our current bounds int delta = widthSize - mTotalLength; if (delta != 0 && totalWeight > 0.0f) { float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; for (int i = 0; i < count; ++i) { final View child = getVirtualChildAt(i); if (child == null || child.getVisibility() == View.GONE) { continue; } final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); float childExtra = lp.weight; if (childExtra > 0) { int share = (int) (childExtra * delta / weightSum); weightSum -= childExtra; delta -= share; int childWidth = child.getMeasuredWidth() + share; if (childWidth < 0) { childWidth = 0; } } } } |
變數含義
widthSize: LinearLayout的寬度
mTotalLength: 所有子View的寬度的和(還沒用考慮layout_weight)
totalWeight: 所有子View的layout_weight的和
mWeihtSUm: LinearLayout的android:weightSum屬性
過程分析:
首先計算出額外空間(可以為負)如果額外空間不為0並且有子View的layout_weight不為0的話按layout_weight分配額外空間:
| 代碼如下 |
複製代碼 |
int delta = widthSize - mTotalLength; if (delta != 0 && totalWeight > 0.0f) { ... } |
如果LinearLayout設定了weightSum則覆蓋子View的layout_weight的和:
| 代碼如下 |
複製代碼 |
float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; |
然後遍曆LinearLayout的子項目,如果不為null且Visibility不為GONE的話,取得它的LayoutParams,如果它的layout_weight大於0,根據weightSum與它的weight計算出分配給它的額外空間
| 代碼如下 |
複製代碼 |
if (childExtra > 0) { int share = (int) (childExtra * delta / weightSum); weightSum -= childExtra; delta -= share; int childWidth = child.getMeasuredWidth() + share; if (childWidth < 0) { childWidth = 0; } } |
例子
| 代碼如下 |
複製代碼 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="redwwwwwww" android:gravity="center_horizontal" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="green" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2"/> <TextView android:text="blue" android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3"/> <TextView android:text="yellow" android:gravity="center_horizontal" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="4"/> </LinearLayout> </LinearLayout>
|