小弟有问题请大虾指教。
有四个button,我想把它们横向排列成两排,一排一个,不在XML中设置,在android代码中实现
Button button  = new Button(this);
button.setText("");
layout.add(button);
这样四个按钮,请问我该怎么做,谢谢了

解决方案 »

  1.   


     RelativeLayout rl = new RelativeLayout(this);
            Button btn1 = new Button(this);
            Button btn2 = new Button(this);
            Button btn3 = new Button(this);
            Button btn4 = new Button(this);
            btn1.setId(111);
            btn2.setId(222);
            btn3.setId(333);
            btn4.setId(444);
            btn1.setText("按钮1");
            btn2.setText("按钮2");
            btn3.setText("按钮3");
            btn4.setText("按钮4");
            rl.addView(btn1);
            rl.addView(btn2);
            rl.addView(btn3);
            rl.addView(btn4);
    //按钮属性的设置。。还真是TM的麻烦啊。。
            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
             RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
             RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
             RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
             RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            btn1.setLayoutParams(lp1);
            lp2.addRule(RelativeLayout.RIGHT_OF, btn1.getId());
            btn2.setLayoutParams(lp2);
            lp3.addRule(RelativeLayout.BELOW,btn1.getId());
            btn3.setLayoutParams(lp3);
            lp4.addRule(RelativeLayout.RIGHT_OF, btn3.getId());
            lp4.addRule(RelativeLayout.BELOW,btn1.getId());
            btn4.setLayoutParams(lp4);
            
            this.setContentView(rl);
            
      

  2.   

    package com.wxj.viewtest;import android.app.Activity;
    import android.os.Bundle;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;public class TestActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(layout());//这里直接应用下面的布局就可以了
    }

    /**
     * 动态布局
     * @return
     */
    private LinearLayout layout(){
    LinearLayout linearMain = new LinearLayout(this);//这个父类布局
    linearMain.setOrientation(LinearLayout.VERTICAL);//布局为垂直的
    linearMain.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


    LinearLayout linearFirstRow = new LinearLayout(this);//这是第一排
    linearFirstRow.setOrientation(LinearLayout.HORIZONTAL);//其实默认的就是水平布局

    LinearLayout linearSecondRow = new LinearLayout(this);//这是第二排
    linearSecondRow.setOrientation(LinearLayout.HORIZONTAL);


    Button btn[] = new Button[4];//四个按钮数组

    for(int i=0;i<4;i++){//循环实例化四个按钮,并相应的赋值
    btn[i] = new Button(this);
    btn[i].setText("btn"+i);
    btn[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

    linearFirstRow.addView(btn[0]);
    linearFirstRow.addView(btn[1]);

    linearSecondRow.addView(btn[2]);
    linearSecondRow.addView(btn[3]);

    linearMain.addView(linearFirstRow);
    linearMain.addView(linearSecondRow);

    return linearMain;
    }
    }
    直接放在工程里面就可以用了