如何以编程方式设置layout_rowWeight和layout_columnWeight

时间:2019-07-09 12:28:19

标签: android android-studio

我在布局中有一个按钮:

<Button
    android:id="@+id/button1"
    android:text="1"
    android:tag="1"
    app:layout_rowWeight="1"
    app:layout_columnWeight="1"/>

现在,我想以编程方式创建此按钮。但是我不知道如何设置:layout_columnWeightlayout_rowWeight

我试图做类似的事情。

button.setLayoutRowWeight(1);
button.setLayoutColumnWeight(1);

但这是错误的代码。

这是我的代码:

GridLayout numsTable = (GridLayout)findViewById(R.id.numsTable);

String[] btn_name = {"1", "2", "3", "4"};

    for (int i = 0; i < btn_name.length; i++) {
        numButton = new Button(new ContextThemeWrapper(this, R.style.numButtons), null, 0);
        numButton.setId(i);
        numButton.setText(btn_name[i]);
        numButton.setTag(i);
        numsTable.addView(numButton);
    }

我想要得到这样的东西:

GridLayout numsTable = (GridLayout)findViewById(R.id.numsTable);

String[] btn_name = {"1", "2", "3", "4"};

    for (int i = 0; i < btn_name.length; i++) {
        numButton = new Button(new ContextThemeWrapper(this, R.style.numButtons), null, 0);
        numButton.setId(i);
        numButton.setText(btn_name[i]);
        numButton.setLayoutRowWeight(1);
        numButton.setLayoutColumnWeight(1);
        numButton.setTag(i);
        numsTable.addView(numButton);
    }

0 个答案:

没有答案