2011-08-03 7 views
0

1. Questiondisposition linéaire (pourcentage de définition - layout_weight) Android

Je voudrais avoir une disposition linéaire plein écran avec 2 lignes, tandis que la première ligne est divisée en 2 sections, où une section occupe 30 % et deuxième 70% de la largeur et la deuxième ligne est divisée en 2 sections de la même taille (chaque 50% de la largeur). De plus, la première ligne n'occupe que 33% de la hauteur de l'écran.

Je pense que l'utilisation du android: layout_weight est la bonne façon. Mais il est peu compliqué car parfois il s'applique à android: layout_width et parfois à android: layout_height et il s'applique à l'un ou l'autre selon les valeurs des attributs XML respectifs.

Après avoir expérimenté, j'ai trouvé que si je mets la valeur d'attribut à wrap_content il est outrepassée par android: layout_weight paramètre, par exemple le code suivant applique le android: layout_weight à android: layout_width:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:background="#FF0000"/> 

Est-ce une bonne approche?

2. Exemple

Pour dévalorisé la mise en page (voir l'image) Je dois faire ceci:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#FF0000"/> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="2" 
     android:background="#00FF00"/> 
    </LinearLayout> 
    <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#0000FF"/> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#0000AA"/>  
    </LinearLayout> 
</LinearLayout> 

enter image description here

Merci Sten

Répondre

1
< LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#FF0000"/> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="2" 
     android:background="#00FF00"/> 
    </LinearLayout> 
    <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#0000FF"/> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#0000AA"/>  
    </LinearLayout> 
</LinearLayout> 
Questions connexes