我怎么能把我的按钮这样?

时间:2013-03-05 22:14:40

标签: android layout

enter image description here

我想把我的号码按钮放在那里。这是我的代码:

LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout layout2 = new LinearLayout(this);
        layout2.setOrientation(LinearLayout.HORIZONTAL);

        TextView titleView = new TextView(this);
        titleView.setText("Hello World!");
        layout.addView(titleView);

        android.widget.LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1);
        android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);

        Button btnConnect = new Button(this);
        btnConnect.setText("Connect");
        btnConnect.setLayoutParams(param);
        layout2.addView(btnConnect);

        Button btnDisconnect = new Button(this);
        btnDisconnect.setText("Disconnect");
        layout2.addView(btnDisconnect);
        btnDisconnect.setLayoutParams(param);


        layout.addView(layout2);

        TableLayout tblLayout = new TableLayout(this);
        tblLayout.setOrientation(TableLayout.HORIZONTAL);
        TableRow tblrow = null;



        for (int i = 1; i <= 9; i++) {
            if (i % 3 == 1) {
                tblrow = new TableRow(this);
                tblLayout.addView(tblrow);

            }

            Button b = new Button(this);
            b.setText("" + i);
            tblrow.addView(b);
        }




        TableRow tr = new TableRow(this);
        Button btnZero = new Button(this);
        btnZero.setText("0");
        Button btnHash = new Button(this);
        btnHash.setText("#");
        Button btnStar = new Button(this);
        btnStar.setText("*");
        tr.addView(btnZero);
        tr.addView(btnHash);
        tr.addView(btnStar);
        tblLayout.addView(tr);




        layout.addView(tblLayout);
        setContentView(layout);

这符合:

enter image description here

为了让我的专栏成为那样。我用了这段代码:

android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);
tblLayout.setLayoutParams(param);

但没有变化。我需要做什么?那个Layoutparams不足以做到吗?

1 个答案:

答案 0 :(得分:2)

您需要将layoutlayout2tblLayout,每个tblRowtr的宽度设置为MATCH_PARENT和每个按钮必须具有相等的layout_weight