2013-02-26 5 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

ответ

3

Быстрые примечания: Вложенные веса приводят к плохой производительности, избегают, если это возможно. Не смешивайте match_parent и fill_parent. Просто используйте match_parent.

Ваша основная проблема заключается в том, что у вас есть layout_height="wrap_content" для вашего внешнего LinearLayout, но тогда у вас есть веса. Вы указываете, что каждый макет ребенка составляет 1/3 от общего числа, но тогда общая сумма должна быть суммой его детей. Должен ли каждый макет ребенка быть 1dp? 100dp? если вы хотите, чтобы каждый был 1/3 всего экрана, вы должны установить layout_height="match_parent" для внешнего LinearLayout.

0

Я согласен с Kabuko, пожалуйста, измените все fill_parent на match_parent.

Я рекомендую изменить высоту до 0dp, как вы упомянули, но установив вес 1 для всех LinearLayout.

Значение веса является «фактором», а не процентом.

Пожалуйста, обратитесь к этой What does android:layout_weight mean? на очищенный пример того, как можно использовать layout_weight