gpt4 book ai didi

Android:以编程方式在 TableLayout 中拉伸(stretch)行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:47 25 4
gpt4 key购买 nike

我正在尝试动态创建一个 TableLayout,它的所有行都被拉伸(stretch),如图所示 in this tutorial .我已经通过 XML 完成了它,但我想从 Activity 中完成它。这是我迄今为止尝试过但没有成功的代码:

public View getTableWithAllRowsStretchedView() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
tableLayout.setWeightSum(4);


for (int i = 0; i < 4; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));

for (int j = 0; j < 4; j++) {
Button button = new Button(this);
final int buttonNumber = (j + i * 4);
button.setText("" + buttonNumber);
button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));

tableRow.addView(button);
}
tableLayout.addView(tableRow);
}

linearLayout.addView(tableLayout);
return linearLayout;
}

非常感谢您。

编辑

Screenshot of what I have

enter image description here

what I expect

enter image description here

最佳答案

您的行需要父级的 LayoutParams:

tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.FILL_PARENT, 1.0f));

关于Android:以编程方式在 TableLayout 中拉伸(stretch)行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com