2013-04-27 68 views
1

我得到了一张拉伸的桌子,我希望我的复选框也显示在表格中的中心位置。在TextView的,我叫:计划中心复选框?

  myTextView.setGravity(Gravity.CENTER); 

而且我还试图将其设置为表参数:

这是我的XML设置表:

<TableLayout 
    android:id="@+id/info" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/linearLayout1" 
    android:layout_margin="6dp" 
    android:stretchColumns="0,1,2,3,4" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:background="@drawable/roundedheader" > 

而且这是我打电话时的代码,因为我创建了每一行:

  //set table margin 
      TableLayout.LayoutParams tableRowParams= new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

      int leftMargin=0,rightMargin=0,bottomMargin=0; 
      int topMargin=5; 

      tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 
      tableRowParams.gravity = Gravity.CENTER; 
      row.setLayoutParams(tableRowParams); 

      // add the TableRow to the TableLayout 
      table.addView(row); 

但是,相同的命令没有对t他复选框。有没有人知道有什么区别,以及我需要做什么。该复选框不在表格行中的任何其他布局中。

TIA

+0

你的xml是怎样的? – 2013-04-27 05:44:25

+0

你可以看到这个(可能) http://stackoverflow.com/questions/5515174/android-how-to-center-view-within-column-of-tablelayout?rq=1 – ashishdhiman2007 2013-04-27 05:59:44

+0

我已经在那个主题中尝试了解决方案 - 没有快乐!普拉卡什我已经更新了这个问题。 – 2013-04-27 06:28:40

回答

1

我建议你之前开始使用TableLayout工作,以检查这些链接:

http://developer.android.com/reference/android/widget/TableLayout.html

http://developer.android.com/reference/android/widget/TableRow.html

http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html

也有一堆例子来说明如何工作的TableLayout来自Andro的API演示ID SDK。

假设你的布局android:stretchColumns="0,1,2,3,4"你至少有5列,但从代码片段我不能假设有多少视图被添加到row。如果视图元素较少,那么您需要指定TableRow.LayoutParamsandroid:layout_span,然后使用该空闲空间。所以,假设你只有一个CheckBox一排只有5列,你需要下一步:

TableRow.LayoutParams rowParams = new TableRow.LayoutParams(); 
rowParams.gravity = Gravity.CENTER; 
rowParams.span = 5; 
checkBox.setLayoutParams(rowParams); 

此外,我建议你使用,而不是从代码中创建XML他们的布局。如果你不能坚持使用xml布局,那么至少应该使用xml编辑器来创建它们,所以你可以从代码中创建它们。