在我的代码中,我动态创建行,对于某些行,我需要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));
如何让欢迎 Textview
填满整行?
答案 0 :(得分:0)
试试这个
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);
}
}