2016-12-11 5 views
0

Ich habe eine ListView, die zwei Elemente in einer Zeile anzeigt. Die View die eine Zeile angezeigt hat eine LinearLayout wie es Wurzel ist:Layoutgewichte in Android LinearLayout funktionieren nicht richtig

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

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp"> 

     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@color/grey" 
      android:gravity="center_vertical"> 

      <TextView 
       android:id="@+id/textview1" 
       style="@style/refinement_button" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:layout_margin="1dp" 
       android:layout_weight="1" 
       android:background="@color/white"/> 
     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp"> 

     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@color/grey" 
      android:gravity="center_vertical"> 

      <TextView 
       android:id="@+id/textview2" 
       style="@style/refinement_button" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:layout_margin="1dp" 
       android:layout_weight="1" 
       android:background="@color/white"/> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

im Editor im Android Studio I

sehen

enter image description here

aber auf einem Gerät, ich sehe

enter image description here

(Bild im Editor neu erstellt, um Zeit zu sparen)

Ich habe dies auf einem Nexus6 mit Android 7.0 und auf einem Nexus 5 mit Android 6.0.1 versucht. Ich nehme an, das ist ein Fehler in Android auf den Telefonen oder im Editor in Android Studio. Der Zweck des Editors ist es, Ihnen zu zeigen, wie Ihr Layout auf einem Gerät aussieht.

Ich frage nicht um Rat, wie man ein richtiges Layout erstellt. Mein einziger Punkt ist, dass sich das, was auf meinem Gerät angezeigt wird, von dem unterscheidet, was ich im Editor in Android Studio sehe, und ich frage mich, warum.

+0

Sie stellen eine Frage und beantworten sie sofort selbst in der gleichen Minute .... Was ist der Sinn dafür? – Opiatefuchs

+1

Wenn Sie eine Frage zu stackoverflow stellen, schlägt stackoverflow vor, dass Sie sofort "Q & A style" beantworten. Ich habe viel Zeit damit verbracht, die Lösung zu finden, konnte das Problem hier nicht finden, also teile ich es mit anderen. – Christine

+0

@Opiatefuchs Und hier ist ein [Beispiel] (http://stackoverflow.com/questions/23353173/unidymy-myapp-has-stopped-how-can-i-solve-this) von einem solchen Fall, den Sie wahrscheinlich wissen. – Onik

Antwort

0

Die ListView in einem Fragment, das ist das Layout:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

<ListView 
    android:id="@+id/product_list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:divider="@null" 
    android:layout_marginTop="30dp" 
    android:fadingEdge="none" 
    android:scrollingCache="false"/> 

Ändern der FrameLayout auf ein LinearLayout hier mein Problem gelöst.

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

Ich würde denken, dass die Gewichte in meinen Layout-Elementen auf der Verpackung des Layout in der Liste Reihe Layout abhängen würden, wie ich in meinem Editor zu sehen. Das Elternelement dieses Layouts scheint jedoch auf einem Gerät eine Rolle zu spielen.

+0

Sie haben sich eigentlich geirrt, es muss ein Root-Layout in jedem XML geben, das in Ihrem Fall zwei LinearLayout ist Hier können Sie mehr lesen https://developer.android.com/guide/topics/resources/layout-resource.html –

+0

Framelayout wird verwendet, wenn Sie die Komponenten in Overlay von einander angezeigt werden möchten, genau wie eine Schließen-Schaltfläche in Imageview –

+0

@Umar Was? Es gibt ein Root-Layout. Worüber redest du? – Christine

0

Änderung des Layout,

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:padding="10dp"> 

     <TextView 
      android:id="@+id/textview1" 
      style="@style/refinement_button" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:background="@drawable/text_bg" /> 

     <TextView 
      android:id="@+id/textview2" 
      android:layout_weight="1" 
      style="@style/refinement_button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:background="@drawable/text_bg" /> 

</LinearLayout> 

eine text_bg.xml Datei in drawable Ordnern erstellen.

text_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <solid android:color="@color/white"/> 
    <stroke android:color="@color/grey" android:width="1dp"/> 

</shape> 

nie die unerwünschten Layouts verwenden ....

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:padding="10dp"> 

    <TextView 
     android:id="@+id/textview1" 
     style="@style/refinement_button" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:background="@drawable/text_bg" /> 

    <TextView 
     android:id="@+id/textview2" 
     android:layout_weight="1" 
     style="@style/refinement_button" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:background="@drawable/text_bg" /> 

</LinearLayout> 

Ergebnisse von Genymotion # Samsung Galaxy Note 3 api 18 # Samsung Galaxy s6 api 21 # Google Nexus 7 api 23

enter image description here

Ich möchte eine Sache wissen, in welchem ​​Gerät Sie haben den Code getestet?

+0

Wie beantwortet das die Frage? Was ich gesehen habe, ist, dass das Layout in meinem Android Studio Editor korrekt war, aber falsch auf meinen Geräten. Das Problem besteht immer noch, wenn ich die Grenzen weglasse. Außerdem stimme ich nicht mit dem überein, was Sie über Layouts sagen. – Christine

+0

Was Sie zeigen, ist was ich gesehen habe. Im Editor ist alles in Ordnung. Gerade auf meinen Geräten sind sie falsch gelaufen. Bitte lesen Sie die Frage, bevor Sie Antworten posten. – Christine

+1

Sie sollten verstehen und versuchen, was andere hier beantworten, um sie nicht einfach zu verwerfen. –

Verwandte Themen