2011-09-10 30 views
1

我想在表格中定義一行。爲了此行,我想增加3列,具有以下寬度:在TableLayout中以編程方式指定列權重

textView1:應該是一樣寬,它的內容

viewDivider:應該是2像素寬

TextView2:應該佔據剩餘區域的表。

但是,我無法以編程方式實現上述佈局。下面是代碼:

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)); 
    } 

    } 

而這裏的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> 

我現在面臨以下幾部件的問題與上面的佈局:

1)textView1佔地寬度的內容。

2)viewDivider佔據了大量的寬度,而不是restriced爲2px

感謝您的幫助。

回答

相關問題