2011-03-08 4 views
14

Je veux créer une table dans android avec plusieurs colonnes. La plupart des exemples que j'ai vus est avec 2 colonnes. (Je suis nouveau à Java et Android.) J'ai besoin de 3-4 colonnes et je devrais pouvoir ajouter les rangées dynamiquement dans la table. Quelqu'un peut-il me fournir un exemple de code. (J'utilise eclipse dans win 7)Comment créer une table sous Android avec plusieurs colonnes?

Répondre

23

Je suppose que vous parlez d'une vue TableLayout et non d'une table dans une base de données?

Si oui, voici un exemple XML d'une table avec trois colonnes et trois lignes.

Chaque élément < TableRow> crée une ligne dans la table et chaque vue à l'intérieur de l'élément crée une "colonne". Je l'ai utilisé TextViews, mais ils peuvent être ImageViews, EditText, etc.

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id = "@+id/RHE" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:padding="5dp"> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/runLabel" 
      android:text="R" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/hitLabel" 
      android:text="H" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/errorLabel" 
      android:text="E" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/visitorRuns" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/visitorHits" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/visitorErrors" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/homeRuns" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/homeHits" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/homeErrors" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 
</TableLayout> 

Pour changer dynamiquement ces derniers dans le code, vous auriez quelque chose comme ceci:

// reference the table layout 
TableLayout tbl = (TableLayout)findViewById(R.id.RHE); 
// delcare a new row 
TableRow newRow = new TableRow(this); 
// add views to the row 
newRow.addView(new TextView(this)); // you would actually want to set properties on this before adding it 
// add the row to the table layout 
tbl.addView(newRow); 
+0

Oui, je veux une vue de disposition de table. Les colonnes ne sont pas correctement positionnées. Comment positionner les colonnes? Comment je peux ajouter les lignes dynamiquement? – narayanpatra

+0

Je ne sais pas exactement ce que vous voulez dire par «pas correctement positionné». Le TableLayout sera dimensionné en fonction de ses propriétés, tout comme les éléments à l'intérieur. Peut-être que si vous avez posté votre xml ainsi que ce que vous espérez voir, nous aurons plus de capacité à répondre à la question. – Peter

+0

J'ai utilisé le code du fichier XML tel qu'il est. Changez simplement les 2 premières lignes. ie narayanpatra

Questions connexes