2013-02-26 47 views
2

我在垂直佈局中有三個水平佈局。我希望這三個水平的孩子佈局佔據相同的空間,所以我給每個孩子增加了權重(0.33)。Android垂直佈局爲兒童分配相同的權重(高度)

我將三個子佈局組件的佈局高度分配給0dp,因爲父佈局是垂直佈局,但子佈局不像預期的那樣佔用相同的高度。

<RelativeLayout 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:background="@drawable/paper" 
    android:paddingLeft="4dp" 
    android:paddingRight="8dp" 
    tools:context=".TestActivity" > 

     <LinearLayout 
      android:id="@+id/scratchLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:id="@+id/topLayout" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="0.33" 
       android:layout_marginLeft="3dp" 
       android:layout_marginRight="3dp" 
       android:orientation="horizontal" > 

       <TextView 
        android:id="@+id/topArea1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@android:color/transparent" 
        android:paddingRight="3dp" 
        android:text="Top Area 2" 
        android:textSize="15dp" /> 

       <TextView 
        android:id="@+id/topArea2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@android:color/transparent" 
        android:paddingRight="3dp" 
        android:text="Top Area 1" 
        android:textSize="15dp" /> 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/centerLayout" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="0.33" 
       android:layout_marginLeft="3dp" 
       android:layout_marginRight="3dp" 
       android:orientation="horizontal" > 

       <TextView 
       android:id="@+id/centerArea1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="3dp" 
       android:text="Center Area 1" /> 

       <TextView 
       android:id="@+id/centerArea2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="3dp" 
       android:text="Center Area 2" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/BottomLayout" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="0.33" 
       android:layout_marginLeft="3dp" 
       android:layout_marginRight="3dp" 
       android:orientation="horizontal" > 

       <TextView 
       android:id="@+id/bottomArea1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="3dp" 
       android:text="Bottom Area 1" /> 

       <TextView 
       android:id="@+id/bottomArea2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="3dp" 
       android:text="Bottom Area 2" /> 

      </LinearLayout> 

     </LinearLayout> 

</RelativeLayout> 
+0

只是注意你,'layout_weight'您垂直的LinearLayout清零例如沒有用途,因爲'layout_weight'不是RelativeLayout的子屬性。 – Aprian 2013-02-26 01:19:40

回答

3

快速筆記:嵌套的權重會導致糟糕的性能,儘可能避免。不要混合使用match_parentfill_parent。只需使用match_parent

你的主要問題是你有layout_height="wrap_content"你的外部LinearLayout,但你有權重。你指定每個孩子的佈局是總數的1/3,但是說總數應該是孩子的總數。每個孩子的佈局應該是1dp? 100dp?如果您希望每個屏幕都是整個屏幕的1/3,則應該爲LinearLayout設置layout_height="match_parent"

0

但是,我同意Kabuko,請將所有fill_parent改爲match_parent。

我建議像上面提到的那樣將高度更改爲0dp,但是將所有LinearLayout的高度設置爲1。

重量值是一個「因素」而不是百分比。

請參閱本What does android:layout_weight mean?爲你怎麼可能layout_weight使用