Android程序化创建TableLayout

4

我学会了如何使用XML文件创建UI。但是请帮助我了解如何在编程中不使用XML文件来创建UI,尤其是对于除LinearLayout之外的其他控件。

3个回答

15
使用以下代码创建TableLayout
TableLayout tbl=new TableLayout(context);

使用以下代码创建表格行:

TableRow tr=new TableRow(context);
中添加View的行
tr.addView(view);

这里的view可能是TextView、EditText或其他类型的视图。

将表格行添加到TableLayout中

tbl.addView(tr);

就像这样,你可以将更多的表格行添加到表格布局中。


它正在正确运行,但在活动屏幕上没有显示任何输出......这是我的代码。 - RAVITEJA SATYAVADA
LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT) TableLayout layout=new TableLayout(this); TableLayout.LayoutParams layoutparams=new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); TableRow tablerow=new TableRow(this); TextView tv=new TextView(this); tv.setText("TableLayout演示"); tv.setLayoutParams(params); EditText et=new EditText(this); et.setHint("输入文本"); et.setLayoutParams(params); tablerow.addView(tv); tablerow.addView(et); layout.addView(tablerow); this.addContentView(layout, layoutparams); - RAVITEJA SATYAVADA
什么是上下文?我对Xamarin和Android都很陌生,我只是使用了布局设计师。 - Hassan Faghihi

4
以下代码示例可在此处查看。
public class tablelayout extends Activity implements OnClickListener {
/** Called when the activity is first created. */

//initialize a button and a counter
Button btn;
int counter = 0;

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

    // setup the layout
    setContentView(R.layout.main);

    // add a click-listener on the button
    btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(this);        

}

// run when the button is clicked
public void onClick(View view) {

    // get a reference for the TableLayout
    TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);

    // create a new TableRow
    TableRow row = new TableRow(this);

    // count the counter up by one
    counter++;

    // create a new TextView
    TextView t = new TextView(this);
    // set the text to "text xx"
    t.setText("text " + counter);

    // create a CheckBox
    CheckBox c = new CheckBox(this);

    // add the TextView and the CheckBox to the new TableRow
    row.addView(t);
    row.addView(c);

    // add the TableRow to the TableLayout
    table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

}

}


0

|*| 使用Java代码创建3 x 3按钮的表格布局:

在tblRowCwtVal中设置行数
在tblColCwtVal中设置列数
在tblAryVar中设置字符串 | 图片

在此示例中,我们为每个表视图使用了按钮。您可以使用TextView | ImageView并进行相应修改。

int tblRowCwtVal = 3;
int tblColCwtVal = 3;
int[][] tblAryVar =
     {
        {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name},
        {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name},
        {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name}
     };

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.srn_nam_uic);

    namRelLyoVar = (RelativeLayout) findViewById(R.id.NamSrnLyoUid);

    TableLayout namTblLyoVar = new TableLayout(this);
    TableLayout.LayoutParams tblLyoRulVar = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    TableRow.LayoutParams btnLyoRulVar = new TableRow.LayoutParams(50,50);

    for(int tblRowIdxVar = 0; tblRowIdxVar < tblRowCwtVal; tblRowIdxVar++)
    {
        TableRow tblRowVar = new TableRow(this);

        for(int tblColIdxVar = 0; tblColIdxVar < tblColCwtVal; tblColIdxVar++)
        {
            Button namIdxBtnVar = new Button(this);
            Drawable DrwablIdxVar = getResources().getDrawable(tblAryVar[tblRowIdxVar][tblColIdxVar]);
            DrwablIdxVar.setColorFilter(Color.rgb(0,128,0), PorterDuff.Mode.SRC_IN);
            namIdxBtnVar.setBackground(DrwablIdxVar);

            tblRowVar.addView(namIdxBtnVar, btnLyoRulVar);
        }
        namTblLyoVar.addView(tblRowVar, tblLyoRulVar);
    }

    namTblLyoVar.setLayoutParams(tblLyoRulVar);
    namRelLyoVar.addView(namTblLyoVar);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接