0

Je voudrais savoir s'il existe un moyen de modifier la largeur de deux colonnes dans une disposition de table (avec trois colonnes au total). Le problème est que chaque colonne a la même largeur et beaucoup d'espace est gaspillé parce que les deux premiers ne contiennent qu'un id et une date tandis que le dernier contient une description multiligne.Modifier la largeur des colonnes à partir du code android

Voici le code dans lequel la mise en page de table est remplie:

tr_head.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT, 
    LayoutParams.WRAP_CONTENT)); 

    tr_head.setBackgroundColor(Color.rgb(0, 230, 230)); 

    TextView lblSystemStoricalHeaderId = new TextView(this); 
    lblSystemStoricalHeaderId.setText("ID  "); 

    tr_head.addView(lblSystemStoricalHeaderId); 

    TextView lblSystemStoricalHeaderData = new TextView(this); 
    lblSystemStoricalHeaderData.setText("Data"); 

    tr_head.addView(lblSystemStoricalHeaderData); 

    TextView lblSystemStoricalHeaderDescription = new TextView(this); 
    lblSystemStoricalHeaderDescription.setText("  Descrizione"); 

    tr_head.addView(lblSystemStoricalHeaderDescription); 

    tr_head.setMinimumHeight(30); 

    tlSystemStorical.addView(tr_head, new TableLayout.LayoutParams(
     LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT)); 

    for(int i=0;i<reportList.length;i++){ 

     TableRow tr = new TableRow(this); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     TextView lblSystemStoricalId = new TextView(this); 
     lblSystemStoricalId.setText(""+reportList[i].getId()+""); 

     tr.addView(lblSystemStoricalId); 

     TextView lblSystemStoricalData = new TextView(this); 
     long dataC = reportList[i].getDate()*1000; 
     java.util.Date df = new java.util.Date(dataC); 
     String vv = new SimpleDateFormat("dd/MM/yyyy").format(df); 
     lblSystemStoricalData.setText(vv); 

     tr.addView(lblSystemStoricalData); 

     TableRow.LayoutParams tv3Params = new TableRow.LayoutParams(); 
     tv3Params.width=0; 
     tv3Params.weight=1; 
     TextView lblSystemStoricalDescription = new TextView(this); 
     lblSystemStoricalDescription.setText(""+reportList[i].getReportDescription()); 
     lblSystemStoricalDescription.setLayoutParams(tv3Params); 

     tr.addView(lblSystemStoricalDescription); 

     tr.setBackgroundResource(R.drawable.border); 
     tr.setMinimumHeight(40); 

     tlSystemStorical.addView(tr, new TableLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
    } 

et c'est le fichier xml:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/scroll_view_system_description" 
    > 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 

     <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:stretchColumns="*" 
      android:id="@+id/tl_system_storical" 
      > 

     </TableLayout> 
    </FrameLayout> 

</ScrollView> 

Répondre

0

Vous pouvez définir width propriété à 0 pour TableRow childr en, mais spécifiez layout_weight pour eux. Donc, vous écririez:

float[] weights = new float[] { 0.2f, 0.2f, 0.6f}; 

TextView lblSystemStoricalId = new TextView(this); 
lblSystemStoricalId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0])) 

0 est pas de largeur pour une vue (parce que vous utilisez des poids), et le réseau weights contient des poids pour vos trois vues.

Ces poids doivent totaliser 1, et chaque poids est le pourcentage de la largeur que la colonne doit prendre. Donc, vous devez attribuer des poids à vos TableRow enfants - lblSystemStoricalId obtiendrait weights[0], lblSystemStoricalData obtiendrait weights[1] et lblSystemStoricalDescription aurait weights[2].

+0

merci qui a fonctionné! – Inoshichi