3

я имею следующую структуру:LinearLayout с LayoutWeight не работает

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/color_brand" 
       android:weightSum="100"> 

    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="40" 
     android:background="@color/color_white"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" 
     android:background="@color/color_black" 
     android:layout_below="@id/top"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="40" 
     android:background="@color/color_white" 
     android:layout_below="@id/middle"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp"/> 

    </LinearLayout> 

</RelativeLayout> 

Я хочу 40-20-40 раскол между раскладками, и я попробовал все, но ничего не похоже на работу. Я попытался добавить пустой вид в линейные макеты, я дал представления в линейном макете вес, но ничего не работает. Может кто-то указать, что я делаю неправильно?

+0

Просто измените родительский макет с RelativeLayout на LinearLayout. Он будет работать –

+0

Весы не работают на RELATIVE LAYOUT в качестве родительского, вы должны использовать LINEAR LAYOUT в качестве родительского. - –

+0

Спасибо, ребята. Не могу поверить, что я этого не понял. – BlackHatSamurai

ответ

3

Изменить ваш главный корень родителю LinearLayout и придать ему вертикальную ориентацию. RelativeLayout не поддерживает weightsum, как вы видите в своем коде, вы определяете 0dp по высоте, поэтому вам нужно сделать свой корневой вид LinearLayout с вертикальной ориентацией, чтобы сделать работу по весу.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/color_brand" 
      android:weightSum="100"> 

    -------- 
</LinearLayout> 
1

Попробуйте

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:background="@color/color_brand"> 

<LinearLayout 
    android:id="@+id/top" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="40" 
    android:background="@color/color_white" 
    > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="10dp" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/middle" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="20" 
    android:background="@color/color_black" 
    android:layout_below="@id/top" 

    > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="10dp" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/bottom" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="40" 
    android:background="@color/color_white" 
    android:layout_below="@id/middle" 

    > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="10dp" 
     /> 

</LinearLayout> 

Ваш родитель Относительный макет, который почему не работает

1

WeightSum работать только с LinearLayout. Таким образом, вы должны изменить своего родителя RelativeLayout на LinearLayout.

Так Измени этот код

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/color_brand" 
       android:weightSum="100"> 

к этому

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/color_brand" 
       android:weightSum="100" 
       android:orientation="vertical"> 

Примечание: добавить orientation в LinearLayout.

1

android:weightSum не является атрибутом RelativeLayout он является атрибутом LinearLayout. Таким образом, вы можете изменить родительский расположение в LinearLayout или вы можете использовать PercentRelativeLayout

фрагмент кода

<android.support.percent.PercentRelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <ImageView 
     app:layout_widthPercent="50%" 
     app:layout_heightPercent="50%" 
     app:layout_marginTopPercent="25%" 
     app:layout_marginLeftPercent="25%"/> 
</android.support.percent.PercentRelativeLayout> 
1

удалить Relative Layout или изменить его на линейные с ориентацией. Это будет работать.

1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="100"> 

    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="40"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" 
     android:background="@color/colorBlack" 
     android:layout_below="@id/top"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="40" 
     android:layout_below="@id/middle"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp"/> 

    </LinearLayout> 

</LinearLayout> 

Используйте это, чтобы решить вашу проблему. И еще одна вещь, когда вы хотите управлять своим макетом в соответствии с весом, тогда вы должны использовать LINEAR LAYOUT, потому что концепция веса не работает в RELATIVE LAYOUT.

0

Try This Для 20-40-20

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="100"> 

    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="40" 
     android:background="@android:color/darker_gray"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="20" 
     android:background="@android:color/black" 
     android:layout_below="@id/top"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="40" 
     android:background="@android:color/darker_gray" 
     android:layout_below="@id/middle"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 

    </LinearLayout> 
</LinearLayout> 

Выход:
enter image description here

Попробуйте для 40-20-40

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="10"> 
    <LinearLayout 
     android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="3" 
     android:background="@android:color/black" 
     android:layout_below="@id/top"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="4" 
     android:background="@android:color/darker_gray"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 

    </LinearLayout> 


    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="3" 
     android:background="@android:color/black" 
     android:layout_below="@id/middle"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 

    </LinearLayout> 

</LinearLayout> 

Выход
enter image description here

3

Необходимо использовать LinearLayout как родительский для использования weightSum, потому что RelativeLayout не поддерживает weightSum. Теперь вам нужно взять LinearLayout вместо RelativeLayout. Вы должны написать свой КОД, как показано ниже.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/color_brand" 
android:orientation="vertical" 
android:weightSum="100"> 

    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="40" 
     android:background="@color/color_white"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_below="@id/top" 
     android:layout_weight="20" 
     android:background="@color/color_black"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_below="@id/middle" 
     android:layout_weight="40" 
     android:background="@color/color_white"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="10dp" /> 

    </LinearLayout> 


</LinearLayout>