Android TableLayout以编程方式

时间:2011-08-29 04:48:42

标签: android android-tablelayout

我学会了如何使用XML文件创建UI。但请帮助我知道如何在不使用XML文件的情况下以编程方式执行此操作,尤其是对于LinearLayout以外的其他文件。

3 个答案:

答案 0 :(得分:14)

使用以下代码创建TableLayout

TableLayout tbl=new TableLayout(context);

使用以下内容创建表格行

TableRow tr=new TableRow(context);

将View添加到表格行

tr.addView(view);

此处视图可能是TextView或EditText,也可能是等等。

将Table Row添加到TableLayout

tbl.addView(tr);

就像那样,您可以在表格布局中添加更多表格行。

答案 1 :(得分:4)

以下代码示例为Here

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));

}

}

答案 2 :(得分: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);
}