0

在此处输入图像描述

我想把我的数字按钮那样。这是我的代码:

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);

这符合:

在此处输入图像描述

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

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

但没有任何变化。我需要做什么?Layoutparams 还不足以做到这一点吗?

4

1 回答 1

2

您需要设置 、 、 、 each 和 to 的宽度layoutlayout2并且tblLayout每个tblRowButtontr必须MATCH_PARENT具有相等的layout_weight

于 2013-03-05T22:18:34.127 回答