【问题标题】:tablelayout in java code for dynamic UI android app用于动态 UI android 应用程序的 java 代码中的 tablelayout
【发布时间】:2015-02-17 00:11:29
【问题描述】:

我尝试在 tablelayout 中编写动态 UI,但图层是重复的。任何主体都有用于 tablelayout 的示例 javacode,或者可以更正我的代码。我首先编写 2 个布局参数,然后是 tablelayout 和 tablerow,然后是 imagebuttons,然后是 tablerow2,然后在那个 imagebutton 中最后我调用了所有的 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);

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    您不使用“超级表”变量。

    从您的代码中删除:

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

    并添加:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多