3

arbeitete ich folgendes Layout haben:Linearlayout mit LayoutWeight nicht

<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> 

Ich möchte eine 40-20-40 Spaltung zwischen den Layouts, und ich habe alles versucht, aber nichts scheint zu funktionieren. Ich habe versucht, eine leere Ansicht in den linearen Layouts hinzuzufügen, ich habe den Ansichten im linearen Layout das Gewicht gegeben, aber nichts funktioniert. Kann jemand darauf hinweisen, was ich falsch mache?

+0

Ändern Sie einfach Ihr übergeordnetes Layout von RelativeLayout zu LinearLayout. Es funktioniert –

+0

Gewichte funktionieren nicht auf RELATIVE LAYOUT als Eltern, Sie müssen LINEAR LAYOUT als Eltern verwenden. - –

+0

Danke Jungs. Ich kann nicht glauben, dass ich das nicht verstanden habe. – BlackHatSamurai

Antwort

3

Ändern Sie Ihre Hauptwurzel Eltern LinearLayout funktioniert und es eine vertikale Ausrichtung geben. RelativeLayout unterstützt weightsum nicht. Wie Sie in Ihrem Code sehen, definieren Sie 0dp für die Höhe, so dass Sie Ihre Stammansicht LinearLayout mit vertikaler Ausrichtung erstellen müssen, um die Gewichtungsarbeit zu leisten.

<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

diese

<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> 

Ihre Eltern ist Relative Layout Versuchen Sie, warum nicht

1

WeightSum nur mit dem LinearLayout arbeiten. Also müssen Sie Ihre Eltern RelativeLayout zu LinearLayout ändern.

So ändern Sie diesen Code

<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"> 

dieser

<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"> 

Hinweis: orientation im LinearLayout hinzuzufügen.

1

android:weightSum ist kein Attribut von RelativeLayout es ein Attribut LinearLayout ist. So können Sie das übergeordnete Layout LinearLayout ändern oder Sie können PercentRelativeLayout

Code-Schnipsel

<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

entfernen Ihre Relative-Layout verwenden oder ändern Sie es mit Ihrer Orientierung linear. Es wird klappen.

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> 

Verwenden Sie dies wird Ihr Problem lösen. Und noch eine Sache, wenn Sie Ihr Layout nach Gewicht verwalten möchten, dann müssen Sie LINEAR LAYOUT verwenden, da das Gewichtskonzept in RELATIVE LAYOUT nicht funktioniert.

0

Try This Für 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> 

Ausgang:
enter image description here

die Sie interessieren für 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> 

Ausgabe
enter image description here

3

Sie müssen LinearLayout als übergeordnetes Element für die Verwendung von weightSum verwenden, da RelativeLayout weightSum nicht unterstützt. Jetzt müssen Sie LinearLayout anstelle von RelativeLayout nehmen. Sie müssen Ihren CODE wie folgt schreiben.

<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> 
Verwandte Themen