2010-05-26 5 views
13

Je suis en train d'affecter la largeur par rapport aux colonnes dans un ListView qui est dans un TabHost, en utilisant layout_weight comme suggéré here:problème de mise en page Android - largeurs relatives en pour cent en utilisant le poids

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TabWidget android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <TableLayout 
       android:id="@+id/triplist" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="4px"> 
       <TableRow> 
       <ListView android:id="@+id/triplistview" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 
       </TableRow> 
       <TableRow> 
       <Button android:id="@+id/newtripbutton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Add Trip"/> 
       </TableRow> 
[other tabs ...] 

Ma définition de la ligne a 4 colonnes que je voudrais à la taille comme suit:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="1.0" 
    android:padding="4px"> 
    <TextView android:id="@+id/rowtripdate" 
     android:layout_weight=".2" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:inputType="date"/> 
    <TextView android:id="@+id/rowodostart" 
     android:layout_weight=".2" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/rowodoend" 
     android:layout_weight=".2" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/rowcomment" 
     android:layout_weight=".4" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content"> 

Malheureusement, il semble vouloir s'adapter à toutes les colonnes dans l'espace que le b utton occupe, par opposition à la largeur de l'écran. Ou peut-être y a-t-il une autre contrainte que je ne comprends pas. J'apprécierais votre aide.

alt text http://heeroz.com/layoutprob.png

Répondre

11

Je pense que j'ai oublié cette propriété pour l'élément TableLayout:

android:stretchColumns="0" 

Il semble fonctionner maintenant.

+7

Je passerais de px à dip dans vos mises en page pour prendre en charge différentes résolutions d'écran. –

1

Je ne vois rien de mal à ce que de toute évidence vous montrez là. Pour avoir une meilleure idée de ce qui se passe, vous pouvez essayer le heirarchy viewer. Il vient avec le SDK. Il visualisera pour vous combien d'espace chacune des couches "cachées" prend, de sorte que vous pouvez déterminer lequel a décidé d'être aussi grand.

+0

Merci pour l'indice. Je n'avais pas connu cet outil. J'ai trouvé que le ListView (136px) ne remplit pas toute la largeur de la ligne du tableau (480px), mais pourquoi est une question différente. 136px arrive aussi à être la largeur du bouton. – cdonner