2012-12-31 5 views
0

J'ai un LinearLayout avec plusieurs enfants et je veux le ListView pour remplir la moitié inférieure restante de la mise en page de parent:Poids en LinearLayout

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

    <AutoCompleteTextView 
     android:id="@+id/from_tv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="20dp" 
     android:layout_marginBottom="10dp" 
     android:hint="@string/hint_from" 
     android:singleLine="true" 
     android:imeOptions="actionNext" 
     /> 

    <AutoCompleteTextView 
     android:id="@+id/to_tv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:hint="@string/hint_to" 
     android:singleLine="true" 
     android:imeOptions="actionSearch" 
     /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     > 

     <Button 
      android:id="@+id/inverse_btn" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".4" 
      android:layout_margin="10dp" 
      android:text="@string/inverse" 
      android:onClick="onClick" 
      /> 

     <Button 
      android:id="@+id/search_btn" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".6" 
      android:layout_margin="10dp" 
      android:text="@string/search" 
      android:onClick="onClick" 
      /> 


    </LinearLayout> 


    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_marginTop="20dp" 
     android:layout_marginBottom="10dp" 
     android:background="@color/holo_blue_dark" 
     /> 

    <ListView 
     android:id="@+id/favorites_lv" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight=".5" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_gravity="bottom" 
     android:background="#700" 
     /> 

</LinearLayout> 

Mais j'obtenir ce résultat:

enter image description here

En outre, comme vous pouvez le voir, il existe un problème avec la hauteur des éléments ListView. La mise en page:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="5dp"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@android:drawable/ic_menu_mylocation" 
     android:contentDescription="@string/favorites_icon_cd" 
     /> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="20dp" 
     android:orientation="vertical" 
     android:gravity="left|center_vertical" 
     > 

     <TextView 
      android:id="@+id/from_tv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="end" 
      /> 

     <TextView 
      android:id="@+id/to_tv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="end" 
      /> 

    </LinearLayout> 

</LinearLayout> 

Comment résoudre ces problèmes?

Répondre

3

J'ai un LinearLayout avec plusieurs enfants et je veux que le ListView pour remplir la moitié inférieure restante de la mise en page de parent:

Vous pouvez essayer de modifier simplement la hauteur du ListView à match_parent et ne pas utiliser layout_weight:

<ListView 
    android:id="@+id/favorites_lv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    ... /> 

aussi, comme vous pouvez le voir, il y a un problème avec la hauteur des éléments de ListView.

Pour utiliser votre mise en page simplement changer la hauteur du LinearLayout imbriquée à wrap_content:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:orientation="vertical" 
    android:gravity="left|center_vertical" 
    > 

(Si vous voulez que l'image centrée, vous pouvez ajouter. android:layout_gravity="center_vertical" à votre ImageView)

Bien Si vous utilisez un RelativeLayout plutôt que deux LinearLayouts, Android sera plus rapide à dessiner. Essayez ceci:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="20dp" 
     android:src="@android:drawable/ic_menu_mylocation" 
     android:contentDescription="@string/favorites_icon_cd" 
     /> 

    <TextView 
     android:id="@+id/from_tv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@id/image" 
     android:ellipsize="end" 
     /> 

    <TextView 
     android:id="@+id/to_tv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/from_tv" 
     android:layout_toRightOf="@id/image" 
     android:ellipsize="end" 
     /> 

</RelativeLayout> 
+0

Merci, le 1er problème est résolu, et qu'en est-il du 2ème? – arts777

+0

Désolé je n'ai pas vu la deuxième question, j'ai mis à jour ma réponse. – Sam

+0

essayé les deux solutions possibles sans résultat :( – arts777