0

我正在尝试使用 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

4 回答 4

3

为每一行添加一个垂直的 LinearLayout 怎么样?定义 Layout 的大小,并为每个按钮赋予 1 的权重

于 2012-07-18T11:32:54.217 回答
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);
于 2012-07-18T11:34:37.010 回答
0

试试Following,就当做基础Demo吧:

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;

}
}
于 2012-07-18T11:40:58.697 回答
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);
         }
    }
于 2012-07-18T12:00:17.657 回答