0

我正在动态创建按钮。但问题是它们是从顶部创建的。我想从底部创建它。

public class MainActivity extends Activity implements View.OnClickListener {
    int i, j, butNum, lay1num = 1, lay2num = 100, lay3num = 100, store;
    Button[] Button;
    EditText numBut;
    LinearLayout mainLayout;
    LinearLayout[] subLayout;
    LinearLayout.LayoutParams params;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        butNum = 5;
        Button = new Button[butNum];
        subLayout = new LinearLayout[3];
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.HORIZONTAL);
        mainLayout.setWeightSum(90);
        mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        layoutGen();

        for (j = 0; j < butNum; j++) {
            int value = j + 1;
            Button[j] = new Button(this);
            Button[j].setText("" + value);
            Button[j].setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            Button[j].setTextSize(20);
            Button[j].setTag(value);
            Button[j].setId(j);
            subLayout[0].addView(Button[j]);
            Button[j].setOnClickListener(this);
        }

        mainLayout.addView(subLayout[0], params);

        setContentView(mainLayout);
    }

无论如何我可以创建与底部对齐的按钮。

4

1 回答 1

1
subLayout.setGravity(Gravity.BOTTOM);

您不需要将布局大小设置为 match_parent。

最终你可以设置一个 layout_gravity 像这里

于 2013-04-14T00:21:15.517 回答