2016-12-11 3 views
0

J'ai un ListView qui affiche deux éléments d'affilée. Le View qui affiche une ligne a une LinearLayout comme il est la racine:Les poids de mise en page dans Android LinearLayout ne fonctionnent pas correctement

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

Dans l'éditeur dans Android Studio Je vois

enter image description here

mais sur un appareil que je vois

enter image description here

(image recréée dans l'éditeur, pour gagner du temps)

J'ai essayé ceci sur un Nexus6 avec Android 7.0 et sur un Nexus 5 avec Android 6.0.1. Je suppose que c'est un bug dans Android sur les téléphones, ou dans l'éditeur dans Android Studio. Le but de l'éditeur est de vous montrer à quoi ressemble votre mise en page sur un appareil.

Je ne demande pas de conseils sur la façon de créer une mise en page appropriée. Mon seul point est que ce qui apparaît sur mon appareil est différent de ce que je vois dans l'éditeur dans Android Studio et je me demande comment venir.

+0

vous posez une question et vous répondez immédiatement vous-même dans la même minute .... quel est le sens de cela? – Opiatefuchs

+1

Lorsque vous posez une question sur stackoverflow, stackoverflow vous suggère de répondre immédiatement "style Q & A". J'ai passé beaucoup de temps à trouver la solution, je n'ai pas trouvé le problème ici, alors je le partage avec d'autres. – Christine

+0

@Opiatefuchs Et voici un [exemple] (http://stackoverflow.com/questions/23353173/unheureusement-myapp-has-stopped-how-can-i-solve-this) d'un tel cas que vous connaissez probablement. – Onik

Répondre

0

Le ListView est dans un fragment, c'est sa mise en page:

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

Modification du FrameLayout à un LinearLayout ici résolu mon problème.

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

Je pense que les poids dans mes éléments de mise en page dépendrait de la mise en page d'emballage dans la liste mise en page de la ligne, que je vois dans mon éditeur. Cependant, le parent de cette disposition semble importer sur un périphérique.

+0

Vous avez réellement tort, il doit y avoir une mise en page racine dans n'importe quel xml qui dans votre cas est deux LinearLayout. lire plus ici https://developer.android.com/guide/topics/resources/layout-resource.html –

+0

Framelayout est utilisé lorsque vous souhaitez que les composants apparaissent en superposition de l'un à l'autre, tout comme un bouton de fermeture sur une image. –

+0

@Umar Quoi? Il y a une disposition de racine. Qu'est-ce que tu racontes? – Christine

0

changer la mise en page,

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

créer un fichier dans le dossier text_bg.xmldrawable.

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> 

Ne jamais utiliser les indésirables Layouts ....

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> 

Les résultats de Genymotion # Samsung Galaxy Note 3 api 18 # Samsung Galaxy s6 api 21 # Google Nexus 7 api 23

enter image description here

Je veux savoir une chose, dispositif dans lequel vous avez testé le code?

+0

Comment cela répond-il à la question? Ce que j'ai vu, c'est que la mise en page était correcte dans mon éditeur Android Studio, mais mal sur mes appareils. Le problème existe toujours quand je laisse tomber les frontières. De plus, je ne suis pas d'accord avec ce que vous dites sur les mises en page. – Christine

+0

Ce que vous montrez est ce que j'ai vu. Dans l'éditeur, tout va bien. Juste sur mes appareils, ils ont mal tourné. Veuillez lire la question avant de poster des réponses. – Christine

+1

Vous devez comprendre et essayer ce que les autres répondent ici pour ne pas les baisser. –