gpt4 book ai didi

java - 动态设置表格布局

转载 作者:行者123 更新时间:2023-11-30 02:50:10 25 4
gpt4 key购买 nike

我正在尝试在 Activity 中动态设置表格布局。行数和列数取决于用户的输入。

所以问题 - 每次行数都很好但每行中的列数(textViews)总是等于数组的最小值 no_periods[ ]。例如。如果 no_days 的值为 5,则 no_periods[] 有 2、3、4、5、6 个值。该表将有 5 行,但每行将只有 2 个 TextView ,而不是相应行中的 2、3、4、5、6 个 TextView 。

这是我正在尝试的代码

        //loop to set the rows and text view in the table layout of activity_home  
for (int i = 0; i < no_days; i++) {

tableRow = new TableRow(TimeTable.this);

for (int j = 0; j <no_periods[i]; j++) {

textView = new TextView(TimeTable.this);
textView.setHint("Click");
textView.setClickable(true);
textView.setOnClickListener(click_listen);

int maxpixels = screenWidth/(no_periods[i]);
int minpixels = maxpixels;

textView.setPadding(10, 10, 10, 10);

textView.setMaxWidth(maxpixels);
textView.setMinWidth(minpixels);

tableRow.addView(textView);

}
tableLayout.addView(tableRow);

}

最佳答案

不用设置 max 和 minWidth,而是使用 TableRow weight:

textView.setPadding(10, 10, 10, 10);

tableRow.addView(textView, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));

这将在每行的 TextView 之间分配水平空间。

附言。您可能也想使用 textView.setGravity(Gravity.CENTER);

关于java - 动态设置表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24343697/

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