如何在布局上的行中放置动态创建的按钮?

时间:2012-07-18 11:28:27

标签: java android android-layout

我正在尝试使用for循环添加24个按钮。我试图在每行放置四个大小相同的按钮,并希望看到下一组四个按钮出现在它们下面。

我怎样才能做到这一点?

到目前为止我的努力工作看起来像这样:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView sv = new ScrollView(this);
        LinearLayout layout = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                  (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layout.setWeightSum(12.0f);
        params.weight = 3.0f;
        layout.setOrientation(LinearLayout.HORIZONTAL);
        sv.addView(layout);
        for (int i = 0; i < 24; i++) {
            Button btn = new Button(this);
            btn.setText("Button");
            btn.setLayoutParams(params);
            layout.addView(btn);
        }
        this.setContentView(sv);
    }

任何帮助表示感谢。

4 个答案:

答案 0 :(得分:3)

如何为每一行添加垂直LinearLayout? 定义布局的大小,并为每个按钮赋予1

的权重

答案 1 :(得分:0)

这就是您想要添加Buttons的方式:

    layout.setOrientation(LinearLayout.VERTICAL);
    sv.addView(layout);
    for (int i = 0; i < 6; i++) {
         Linearlayout row = new LinearLayout(this);
         row.setLayoutParams(new LinearLayout.LayoutParams
                  (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));    
         for (int  j = 0; j < 4; j++) {
                Button btn = new Button(this);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
                  (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                lp.weight = 1; 
                btn.setLayoutParams(lp); 
                btn.setText("Button");
                btn.setLayoutParams(params);
                row.addView(btn);
         }
         layout.addView(row);
    }
    this.setContentView(sv);

答案 2 :(得分:0)

尝试关注,只需将其作为基本演示:

public class ABCActivity extends Activity {
int myid = 2001;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout scrollHolder = new RelativeLayout(this);
    scrollHolder.setId(++myid);
    RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    scrollHolder.setLayoutParams(scrollHolderParams);
    scrollHolder.setBackgroundColor(Color.DKGRAY);
    scrollHolder.setGravity(Gravity.CENTER_VERTICAL);

    // ScrollView
    ScrollView scroll = new ScrollView(this);
    scroll.setId(++myid);
    scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    scroll.setBackgroundColor(Color.GREEN);

    // RelativeLayout
    RelativeLayout etHolder = new RelativeLayout(this);
    etHolder.setId(++myid);
    RelativeLayout.LayoutParams etHolderParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    etHolder.setLayoutParams(etHolderParams);
    etHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    etHolder.setPadding(0, 0, 0, 25);

    // Creating EditTextes and Adding to etHolder AS you have done
    RelativeLayout rl2 = addEditText(etHolder, null, 1);
    RelativeLayout rl3 = addEditText(etHolder, rl2, 2);
    RelativeLayout rl4 = addEditText(etHolder, rl3, 3);
    RelativeLayout rl5 = addEditText(etHolder, rl4, 4);
    RelativeLayout rl6 = addEditText(etHolder, rl5, 5);
    RelativeLayout rl7 = addEditText(etHolder, rl6, 6);
    RelativeLayout rl8 = addEditText(etHolder, rl7, 7);
    RelativeLayout rl9 = addEditText(etHolder, rl8, 8);
    RelativeLayout rl10 = addEditText(etHolder, rl9, 9);
    RelativeLayout rl11 = addEditText(etHolder, rl10, 10);
    RelativeLayout rl12 = addEditText(etHolder, rl11, 11);
    RelativeLayout rl13 = addEditText(etHolder, rl12, 12);

    scroll.addView(etHolder); // adding RelativeLayout = etHolder to ScrollView = scroll

    scrollHolder.addView(scroll); // adding ScrollView = scroll to RelativeLayout = scrollHolder

    setContentView(scrollHolder);

}

// Method as you have done
private RelativeLayout addEditText(RelativeLayout objRLContent,
        RelativeLayout layoutAbove, int i) {

    RelativeLayout objRLEditText = new RelativeLayout(this);
    objRLEditText.setId(1100 + i);
    RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    if (layoutAbove != null)
        objRLEditTextParams.addRule(RelativeLayout.BELOW,
                layoutAbove.getId());
    objRLEditText.setLayoutParams(objRLEditTextParams);
    objRLEditText.setPadding(8, 2, 8, 2);

    Button objBtn = new Button(this);
    objBtn.setId(1300 + i);
    RelativeLayout.LayoutParams objETDataParams = new RelativeLayout.LayoutParams(
            200, LayoutParams.WRAP_CONTENT);
    objBtn.setLayoutParams(objETDataParams);
    objBtn.setPadding(8, 0, 8, 0);
    objBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    objBtn.setText("" + i);
    objBtn.setSingleLine(true);
    objRLEditText.addView(objBtn);
    objRLContent.addView(objRLEditText);

    return objRLEditText;

}
}

答案 3 :(得分:0)

您可以使用表格布局或网格布局

    TableLayout table = (TableLayout)findViewById(R.id.the_table);

    for(int j=0;j<3;j++){
         TableRow row = new TableRow(this);

         for (int i=0; i<4;i++){
              final Button Button = new Button(this);
              Button.setTag(row.getTag());
              Button.setText("x");
              Button.setWidth(30);
              Button.setHeight(10);
              row_UE.addView(Button);
         }
    }