2012-02-25 70 views
0

我正嘗試使用從數據庫中提取的值將表格佈局添加到表格佈局中。所以,表格行將被動態添加。 我想要一個很好的2列布局和每個中心對齊的文本。我使用下面的代碼。但它不起作用。任何人都可以請幫忙。如何在Android中動態添加表格行時正確對齊表格佈局

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/display_table" > 
<TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="1dp" 
     > 

     <TextView android:background="@drawable/cell_shape" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:gravity="center" 
      android:text=""/> 
     <TextView android:background="@drawable/cell_shape" 
      android:textAppearance="?android:attr/textAppearanceMedium" android:text="" 
      android:gravity="center"/> 
    </TableRow> 

    </TableLayout> 

我正在將行添加到佈局如下。

private void addHeaderRow(TableLayout tl){ 
    TableRow tr = new TableRow(this); 
    tr.setPadding(1, 1, 1, 1); 

    tr.setLayoutParams(new LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 
     /* Create a Button to be the row-content. */ 
     TextView text1 = new TextView(this); 
     TextView text2 = new TextView(this); 

     text1.setText(in.toUpperCase()); 
     text2.setText(out.toUpperCase()); 

     text1.setGravity(Gravity.LEFT); 
     text2.setGravity(Gravity.CENTER); 
     tr.addView(text1); 
     tr.addView(text2); 
    /* Add row to TableLayout. */ 
    tl.addView(tr,new TableLayout.LayoutParams(
     LayoutParams.FILL_PARENT, 
     LayoutParams.WRAP_CONTENT)); 
    } 

回答

1

這種嘗試...

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/display_table" > 
<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    > 

    <TextView android:layout_width="150dp" 
     android:layout_height="wrap_content" 
    android:background="@drawable/cell_shape" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:gravity="center" 
     android:text=""/> 
    <TextView 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
    android:background="@drawable/cell_shape" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="" 
     android:gravity="center"/> 
</TableRow> 

</TableLayout> 

指定佈局的寬度和高度屬性到烏爾TextView的

+0

謝謝Sandhya!我正在嘗試使用layoutParams將margin設置爲表格行。當我這樣做時,文本視圖顯示在表格行下方。文字瀏覽應該出現在表格行內。對此有什麼要求?提前致謝。 – Harish 2012-02-26 05:17:43

1

把表佈局中滾動查看在XML,因爲你不知道要顯示的行數..

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/display_table" > 
</TableLayout> 
</ScrollView> 

現在您的Java代碼:

class SomeTableLayout extends Activity{ 

private TableLayout tableLayout = null; 
private TableLayout.LayoutParams params= null; 

void onCreate(){ 

tableLayout = (TableLayout)findViewById(R.id.display_table); 

addHeaderRow(); 

} 

private void addHeaderRow(){ 

tableLayout.removeAllViews(); 

for(int i = 0; i < cursor.length(); i++){ 
TableRow tr = new TableRow(this); 
tr.setPadding(1, 1, 1, 1); 
tr.setId(i + 1); 
tr.setOnclickLinstener(new onClickListener(){ 
    // do some datanse operation on click of row. 
}); 

params = new TableLayout.LayoutParams(TableLayout.LayoutParams.fillParent, 
TableLayout.LayoutParams.wrapContent); 

     /* Create a Button to be the row-content. */ 
    TextView text1 = new TextView(this); 
    TextView text2 = new TextView(this); 

    text1.setText(in.toUpperCase()); 
    text2.setText(out.toUpperCase()); 

    text1.setGravity(Gravity.LEFT); 
    text2.setGravity(Gravity.CENTER); 
    tr.addView(text1); 
    tr.addView(text2); 

    /* Add row to TableLayout. */ 
    tl.addView(tr); 
    } 
    } 
} 
+0

謝謝Mohanish!這幫助了我。 – Harish 2012-02-27 03:09:03