如何在android中动态添加表行时正确对齐表布局

如何在android中动态添加表行时正确对齐表布局,android,android-layout,android-widget,tablelayout,Android,Android Layout,Android Widget,Tablelayout,我试图使用从DB获取的值将表行添加到表布局中。因此,将动态添加表行。 我想有一个良好的布局与2列和文本在每个中心对齐。我正在使用下面的代码。但它不起作用。谁能帮忙吗 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android

我试图使用从DB获取的值将表行添加到表布局中。因此,将动态添加表行。 我想有一个良好的布局与2列和文本在每个中心对齐。我正在使用下面的代码。但它不起作用。谁能帮忙吗

<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> 
试试这个

   <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> 


为ur textview指定布局宽度和高度属性,将表格布局放在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);
    }
   }
 }
类SomeTableLayout扩展活动{
私有TableLayout TableLayout=null;
private TableLayout.LayoutParams params=null;
void onCreate(){
tableLayout=(tableLayout)findviewbyd(R.id.display\u table);
addHeaderRow();
}
私有void addHeaderRow(){
tableLayout.removeallview();
对于(int i=0;i
谢谢Sandhya!。我正在尝试使用layoutParams设置表格行的边距。当我这样做时,文本视图显示在表行下方。文本视图应显示在表行内。对这个有什么看法吗?提前谢谢。谢谢莫哈尼什!这对我有帮助。
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);
    }
   }
 }