2014-01-27 37 views
0

看起來像是一種避免Android的LinearLayout上的嵌套權重的方法是將嵌套權重參數設置爲嵌套的LinearLayout。舉例來說,這樣的佈局:Android嵌套體重

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<View 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1.0" 
    android:background="#FF0000" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2.0" > 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="10.0" 
     android:background="#00FF00" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="20.0" 
     android:orientation="vertical" > 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="100.0" 
      android:background="#0000FF" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="200.0" > 

      <View 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1000.0" 
       android:background="#FF0000" /> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="2000.0" 
       android:orientation="vertical" > 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="10000.0" 
        android:background="#00FF00" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="20000.0" 
        android:background="#0000FF" /> 
      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

產生以下: enter image description here 雖然這在很大程度上嵌套和嵌套的權重比父權重更大。 LinearLayout權重參數是否真的停止權重傳播?所有API級別都適用?

編輯

爲了更清楚:我想使用嵌套的權重,而不損失性能,並且不使用其他佈局像RelativeLayout的,我認爲這是一個解決方案,但我不那麼肯定。

+0

不確定我是否理解確切的問題。嵌套的權重不會阻止您達到效果。它只是警告你使用嵌套權重。 –

+0

我想停止權重傳播嵌套/父母佈局,真實的東西,而不是皮棉警告。 – aalmeida

+1

嵌套體重對性能不利。請參閱[this](http://stackoverflow.com/questions/9430764/why-are-nested-weights-bad-for-performance-alternatives) –

回答

0

線性佈局權重僅適用於特定佈局的子項。沒有重量值「傳播」 - 這是重要的兒童的實際大小。

每個加權線性佈局第一測量兒童照常然後執行addional措施/佈局傳遞,在其重量的比例分配在直線佈局給孩子任何剩餘空間。如果所有權重都爲零(如非權重佈局中的情況),則此步驟不是必需的。

爲什麼嵌套權重對性能不利是因爲每個嵌套級別使度量/佈局傳遞數量加倍。例如,在您使用4個加權線性佈局的示例中,將會有2^4 = 16個度量/佈局傳遞。