2012-02-25 18 views
0

DBから値をフェッチしてテーブルレイアウトにテーブル行を追加しようとしています。したがって、表の行は動的に追加されます。 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

Thanks Sandhya! layoutParamsを使用してテーブル行に余白を設定しようとしています。私がそれをしたとき、テキストビューはテーブルの行の下に現れています。 textviewsはテーブル行の内側に表示されます。これに関するどんな説得?前もって感謝します。 – Harish

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

関連する問題