Si j'ai 2 LinearLayouts divisés% 50 /% 50 tout va bien. Les poids sont 1 et 1. Dès que j'ajoute un TextView dans le LinearLayout supérieur, il étire cette disposition. J'utilise "wrap_content" comme la documentation dit que je devrais quand il s'agit de poids. Comme vous pouvez le voir le rouge et le vert devraient être divisés également et le texte sur le fond gris devrait être dans la boîte rouge. Voici le code:TextView à l'intérieur de LinearLayout étire le correctif LinearLayout et fill_parent provoque le basculement de poids
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff0000"
>
<TextView
android:text="@string/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:textColor="#000000"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ff00"
>
</LinearLayout>
Maintenant, si je passe à « remplir parent » comme suit cela fonctionne réellement, mais il crée un autre problème. Voici le code (jusqu'à présent si bon):
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff0000"
>
<TextView
android:text="@string/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:textColor="#000000"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ff00"
>
</LinearLayout>
recherche donc au-dessus nous avons été contraints d'utiliser « fill_parent » et nous penser comme nous résolu le problème, mais ici est le problème si nous utilisons « fill_parent "(Je pris le textview juste pour montrer le problème, textview ne marche pas faire disparaître le problème de toute façon):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#ff0000"
>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00ff00"
>
</LinearLayout>
</LinearLayout>
Comme vous pouvez le voir, j'Affectez les poids 3 (rouge en haut) et 2 (sur fond vert), mais ce qui se passe réellement, c'est qu'ils sont retournés: le rouge devient 2 et le fond devient 3. Il suffit de mesurer les pixels pour voir.
Voici les résultats des 3 codes:
Tout le monde a eu des expériences similaires?
REMARQUE: Pour être clair, chaque fois que la mise en page a été enveloppé dans ce (la mise en page en haut):
android:layout_width="fill_parent"
android:layout_height="fill_parent"
avec xml version = "1.0" encoding = "utf-8" et bon espace de nommage.
En fait, quel est le problème? Votre code semble fonctionner comme il est censé le faire. –
TextView est à l'intérieur de LinearLayout, alors pourquoi étirer le LinearLayout? – Droidamature