2011-05-11 6 views
4

J'essaie de construire une mise en page android en utilisant layout_weight pour s'adapter à toutes les tailles de dispositifs et j'ai du mal à comprendre son comportement.Android layout_weight behavment

J'ai remarqué que le changement layout_width/layout_height a influencé le comportement layout_weight, mais je ne comprends pas comment.

Disons par exemple que je veux avoir un LinearLayout divisé en trois inners verticale LinearLayout telle que celle en haut et un en bas comblent 25% de l'écran, et dans les 50% du milieu, et cela ne devrait pas dépendre du contenu des dispositions intérieures. (Si le contenu d'un LinearLayout intérieur est trop grand, il ne devrait pas déplacer les autres mises en page)

Pour ce faire, devrais-je définir l'attribut layout_height de la LinearLayout intérieure à fill_parent ou wrap_content?

Merci!

EDIT: Il semble que le poids_dispositif soit inversement proportionnel à la taille de la disposition.

3 exemples:

Poids 1/1/1 (travail comme je m'y attendais)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

Résultats: enter image description here

Poids 1/2/1 (Pourquoi, oh pourquoi?)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

Résultats: enter image description here

** Poids 3/2/3 (Ce que j'entended à voir avec 1/2/1):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

Résultats: enter image description here

Répondre

1

Vous devez définir le layout_height des trois dispositions internes à "fill_parent", puis changez leur poids pour les faire ressembler à ce que vous voulez.

+0

@Egor: J'ai essayé de le faire, mais je ne comprends pas comment 'layout_weight' agit dans ce cas: Si je mets 1/1/1, les trois parties seront égales. Mais si je mets 1/2/1, je ne vois pas le second, et l'écran est juste divisé avec 50% de la première mise en page et 50% de la troisième mise en page. Comment ça marche? – nbarraille

+0

@nbarraille: vous devez afficher la mise en page en question afin que nous puissions mieux évaluer ce qui ne va pas dans ce cas particulier, car l'espace doit être distribué comme vous le spécifiez. – dmon

+0

@dmon: J'ai mis à jour ma publication initiale avec des exemples. – nbarraille

3

Vous pouvez essayer de mettre layout_height = 0dp

3

Pour le layout_weight travailler comme prévu (et documenté), vous devez pas régler le layout_height ni à fill_parent ni match_parent.

L'exemple à droite sur http://developer.android.com/guide/topics/ui/layout/linear.html#Weight montre un exemple où layout_height est défini sur 0dp. Toutefois, si vous ne le définissez pas du tout ou si vous le définissez sur wrap_content, le layout_weight fonctionne également comme prévu.

+0

Après des heures passées cette solution a fonctionné! – Massimo

Questions connexes