如何在android tablelayout中拉伸第一列填充屏幕?

时间:2013-11-20 05:53:13

标签: android tablelayout

在我的Android应用程序中,我有一个2列的表格布局。

我想尽可能地拉伸第一列宽度以填充屏幕。但是在这段代码中,它只是尽可能地包装宽度......

有谁知道这里有什么问题?

由于

    TableLayout mytable = (TableLayout)findViewById(R.id.studentContainer);

    TableRow tablerow = new TableRow(this);
    tablerow.setOrientation(TableRow.VERTICAL);
    EditText g = new EditText(this);
    g.setText("AAAAA");
    g.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    EditText g2 = new EditText(this);
    g2.setText("BBBBB");
    g2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    tablerow.addView(g);
    tablerow.addView(g2);
    mytable.addView(tablerow, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

    TableRow tablerow2 = new TableRow(this);
    tablerow2.setOrientation(TableRow.VERTICAL);
    EditText g4 = new EditText(this);
    g4.setText("AAAAA");
    g4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    EditText g5 = new EditText(this);
    g5.setText("BBBBB");
    g5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    tablerow2.addView(g4);
    tablerow2.addView(g5);
    mytable.addView(tablerow2, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

XML

            <TableLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="0"
                android:orientation="vertical"
                android:id="@+id/studentContainer" >
            </TableLayout >

2 个答案:

答案 0 :(得分:0)

做这样的事情:

table layout

  TableLayout tableLayout = new TableLayout(this);
    tableLayout.setStretchAllColumns(true);
    tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));

table row

tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));

这将使table row填充其parent table layout

答案 1 :(得分:0)

I want to stretch the first column width to fill the screen

如果使用view to xml

使用android:layout_span="2"以便宽度填充屏幕。这取决于多少列。因此填充屏幕可能是最大列数,即Fill_Parent

If you are setting layout_span through code

使用

TableRow.LayoutParams params = (TableRow.LayoutParams)editText.getLayoutParams();
params.span = toSpan; say 2,3
editText.setLayoutParams(params);