Android-动态ColSpan不工作

Android-动态ColSpan不工作,android,android-layout,android-tablelayout,Android,Android Layout,Android Tablelayout,我的一个活动中有两个表行。第一行始终包含2个textview,第二行可能包含一个或两个textview(取决于用户输入)。每当我在第二行有一个textview时,我都会尝试将它跨到两列。我正试着这样做 TableRow.LayoutParams param = (LayoutParams)editText.getLayoutParams(); param.span = 2; txtView.setLayoutParams(param); 但它不起作用。发生的情况是,第一行的第二个txtview

我的一个活动中有两个表行。第一行始终包含2个textview,第二行可能包含一个或两个textview(取决于用户输入)。每当我在第二行有一个textview时,我都会尝试将它跨到两列。我正试着这样做

TableRow.LayoutParams param = (LayoutParams)editText.getLayoutParams();
param.span = 2;
txtView.setLayoutParams(param);

但它不起作用。发生的情况是,第一行的第二个txtview被推出屏幕。这里有人能告诉我哪里出了错吗?

我举了这个例子,希望能对你有所帮助

 TableLayout table = (TableLayout)findViewById(R.id.table_id_in_xml);
    TableRow row = new TableRow(this);
    TableRow row2 = new TableRow(this);
    TextView col1, col2, col3;        

    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

    col1 = new TextView(this);
    col2 = new TextView(this);
    col3 = new TextView(this);

    col1.setText("col1");
    col2.setText("col2");
    col3.setText("col3");

    row.addView(col1);
    row.addView(col2);
    row.addView(col3);

    table.addView(row, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //HERE IS WHAT YOU NEED
    TableRow.LayoutParams params = (TableRow.LayoutParams) row2.getLayoutParams();
    params.span = 3; //amount of columns you will span

    col1 = new TextView(this);

    col1.setText("span on col1 makes it bigger than the others");
    col1.setLayoutParams(params);
    row2.addView(col1);

    table.addView(row2, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

我举了这个例子,希望对你有所帮助

 TableLayout table = (TableLayout)findViewById(R.id.table_id_in_xml);
    TableRow row = new TableRow(this);
    TableRow row2 = new TableRow(this);
    TextView col1, col2, col3;        

    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

    col1 = new TextView(this);
    col2 = new TextView(this);
    col3 = new TextView(this);

    col1.setText("col1");
    col2.setText("col2");
    col3.setText("col3");

    row.addView(col1);
    row.addView(col2);
    row.addView(col3);

    table.addView(row, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //HERE IS WHAT YOU NEED
    TableRow.LayoutParams params = (TableRow.LayoutParams) row2.getLayoutParams();
    params.span = 3; //amount of columns you will span

    col1 = new TextView(this);

    col1.setText("span on col1 makes it bigger than the others");
    col1.setLayoutParams(params);
    row2.addView(col1);

    table.addView(row2, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));