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

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

4

1 回答 1

1

I recommend you before start working with TableLayout to check these links:

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

Also there is bunch of examples how to work with TableLayout in API Demos from Android SDK.

Assuming your layout android:stretchColumns="0,1,2,3,4" you will have at least 5 columns, but from the code snippet I can't assume how many views are added to row. You will need to specify TableRow.LayoutParams android:layout_span if there are less view elements then columns and you want to use that free space. So assuming that you have only one CheckBox in a row and only 5 columns you will need to do next:

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

Also I recommend you to use xml layouts instead of creating them from the code. If you can't stick with xml layouts then at least create them using xml editor, so you will have good start point for creating them from the code.

于 2013-04-27T10:04:14.007 回答