Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TextView以编程方式填充整行_Android_Textview_Tablerow - Fatal编程技术网

Android TextView以编程方式填充整行

Android TextView以编程方式填充整行,android,textview,tablerow,Android,Textview,Tablerow,在我的代码中,我动态创建行,对于某些行,我需要TextView填充整行而不是第一个单元格,即: -------------------- | Welcome | |------------------| |cell1|cell2|cell3 | |------------------| |cell1|cell2|cell3 | -------------------- 我的代码: TableRow welcome_row= new TableRow(getBaseContex

在我的代码中,我动态创建行,对于某些行,我需要
TextView
填充整行而不是第一个单元格,即:

--------------------
|    Welcome       |
|------------------|
|cell1|cell2|cell3 |
|------------------|
|cell1|cell2|cell3 |
--------------------
我的代码:

TableRow welcome_row= new TableRow(getBaseContext());
TextView welcome= new TextView(getBaseContext());
welcome.setText("Welcome");
table.addView(welcome_row,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
如何让welcome
Textview
填满整行?

试试这个

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

        TableRow welcome_row = new TableRow(this);
        welcome_row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        welcome_row.setGravity(Gravity.CENTER);
        welcome_row.setBackgroundColor(Color.GRAY);

        TextView welcome = new TextView(this);
        welcome.setText("Welcome!");
        welcome.setBackgroundColor(Color.GREEN);

        // add textview to table row
        welcome_row.addView(welcome);

        // add table row to table
        tableLayout.addView(welcome_row);

        setContentView(tableLayout);
    }
}
参考此