动态 UI android 应用的 java 代码中的表格布局

tablelayout in java code for dynamic UI android app

我尝试在 tablelayout 中编写动态 UI,但层是重复的 together.any body 有 tablelayout 的示例 javacode 或者可以更正我的 codes.i 先写 2 layoutparams,然后是 tablelayout 和 tablerow,然后是 imagebuttons tablerow2 和最后的那个图像按钮我调用所有 imagebuttons.thanks

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.main);
    LayoutParams params =
            new TableRow.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT,1.0f);
    LayoutParams superparams=
            new TableLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);

    TableLayout supertable=new TableLayout(this);
    supertable.setLayoutParams(superparams);
    supertable.setOrientation(TableLayout.VERTICAL);

    TableRow table = new TableRow(this);

    ImageButton ib = new ImageButton(this);
    ib.setImageResource(R.drawable.one);
    ib.setLayoutParams(params);
    ib.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v){
        Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();
        }
    });

    ImageButton ib2 = new ImageButton(this);
    ib2.setImageResource(R.drawable.two);
    ib2.setLayoutParams(params);
    ib2.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v){
        Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();

        }
    });

    ImageButton ib3 = new ImageButton(this);
    ib3.setImageResource(R.drawable.three);
    ib3.setLayoutParams(params);
    ib3.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v){
        Toast.makeText(getBaseContex(),"String",Toast.LENGTH_SHORT).show();
        }
    });

    ImageButton ib4 = new ImageButton(this);
    ib4.setImageResource(R.drawable.four);
    ib4.setLayoutParams(params);
    ib4.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v){

        Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();

    });

    LayoutParams params2 =
            new TableRow.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT,1.0f);

    TableRow table2 = new TableRow(this);


    ImageButton ib5 = new ImageButton(this);
    ib5.setImageResource(R.drawable.five);
    ib5.setLayoutParams(params2);
    ib5.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v){
        Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();
                       }
    });
    table.addView(ib);
    table.addView(ib2);
    table.addView(ib3);
    table.addView(ib4);
    table2.addView(ib5);

    TableRow.LayoutParams layoutParams=
            new TableRow.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT );
    this.addContentView(table, layoutParams);
    TableRow.LayoutParams layoutParams2=
            new TableRow.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT );
    this.addContentView(table2,layoutParams2);

您没有使用 "supertable" 变量。

从您的代码中删除:

this.addContentView(table, layoutParams);
this.addContentView(table2,layoutParams2);

并添加:

supertable.addView(table);
supertable.addView(table2);
setContentView(supertable);