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:
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:
** 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:
@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
@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
@dmon: J'ai mis à jour ma publication initiale avec des exemples. – nbarraille