2011-09-10 2 views
1

Je souhaite définir une seule ligne dans ma table. Pour cette ligne, je veux ajouter 3 colonnes, avec les largeurs suivantes:Affectez par programme un poids de colonne dans une ligne, dans TableLayout

textView1: devrait être aussi large que son contenu

viewDivider: devrait être 2px large

TextView2: devrait occuper la zone restante du tablerow.

Cependant, je ne suis pas en mesure de réaliser la mise en page ci-dessus, par programmation. Voici le code:

public class Copy_2_of_Test extends Activity { 
TableLayout tableLayout = null; 
TextView textView1 = null; 
TextView textView2 = null; 
View viewDivider = null; 

TableLayout tab = null; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView (R.layout.test); 
     tableLayout = (TableLayout)this.findViewById(R.id.mainTable); 
     addViews(); 
     } 

     private void addViews() { 
     // table row with layout params of type TableLayout.LayoutParams 
     TableRow tr = new TableRow(this); 
     tr.setLayoutParams(new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.FILL_PARENT)); 

     // Textview 1 with layout params of type TableRow.LayoutParams 
     textView1 = new TextView(this); 
     textView1.setText("Value1"); 
     textView1.setTextColor(Color.BLACK); 
     textView1.setBackgroundColor(Color.YELLOW); 
     textView1.setLayoutParams(new TableRow.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.FILL_PARENT)); 
     tr.addView(textView1); 

     viewDivider = new View (this); 
     viewDivider.setLayoutParams(new TableRow.LayoutParams(
          2, 
          LayoutParams.FILL_PARENT)); 
     viewDivider.setBackgroundColor(Color.MAGENTA); 
     tr.addView(viewDivider); 


     // Textview 2 with layout params of type TableRow.LayoutParams 
     textView2 = new TextView(this); 
     textView2.setText("Value2 Value2 Value2 Value2 "); 
     textView2.setTextColor(Color.BLACK); 
     textView2.setBackgroundColor(Color.YELLOW); 
     textView2.setLayoutParams(new TableRow.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.FILL_PARENT)); 
     tr.addView(textView2); 

     // Add row to TableLayout. 
     tableLayout.addView(tr,new TableLayout.LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 
    } 

    } 

Et voici test.xml:

 <?xml version="1.0" encoding="utf-8"?> 
    <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="*" 
     android:background="@color/black" 
     android:id="@+id/mainTable"> 

    </TableLayout> 

Je suis confronté à des problèmes thefollowing avec la disposition ci-dessus:

1) textView1 occupe plus large que son contenu.

2) viewDivider occupe beaucoup de largeur et n'est pas restriced à 2px

Merci pour l'aide.

Répondre

Questions connexes