gpt4 book ai didi

android - 宽度列表格布局动态

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:27 25 4
gpt4 key购买 nike

我有一个关于不在 TableLayout 中使用 XML 的问题。有人已经问过和我一样的问题,但提供的答案无法帮助我。 how to set width of column dynamically in android tablelayout?

现在我有这个

TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));

/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */

tr.addView(b);
Button a = new Button(this);
a.setText("Dynamic Button");
a.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
a.setGravity(2);

tr.addView(a);

但我没看到变化有多大

tr.setLayoutParams

将完成例如第一个按钮列 70% 和另一个按钮 30% 的工作

最佳答案

您可以创建一个带有权重的线性布局,并将这两个按钮保留在线性布局中,然后将线性设置为您的表格行。

检查下面的代码:

TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */

TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);

TableRow tr = new TableRow(this);
tr.setLayoutParams(params);

params = new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
params.weight = 1;
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.WHITE);
layout.setWeightSum(1);

/* Create a Button to be the row-content. */

LinearLayout.LayoutParams chiledParams = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams.weight = (float) 0.7;
Button b = new Button(this);
b.setText("Button");
b.setLayoutParams(chiledParams);

/* Add Button to row. */


LinearLayout.LayoutParams chiledParams1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams1.weight = (float) 0.3;
Button a = new Button(this);
a.setText("Button");
a.setLayoutParams(chiledParams1);

layout.addView(b);
layout.addView(a);
tr.addView(layout);
tl.addView(tr);

关于android - 宽度列表格布局动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898746/

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