gpt4 book ai didi

android - 如何在表格布局中动态创建列?

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

我正在尝试了解如何将列和行动态添加到表格布局。

我有这个简单的例子。但是,它在运行时仅显示第一列。

有人可以告诉我为了显示四列而不是一列缺少什么吗?

package com.apollo.testtablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestTableLayoutActivity extends Activity {
/** Called when the activity is first created. */

String col1;
String col2;
String col3;
String col4;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);

for (int x = 1; x < 10; x++) {

col1 = "(" + x + ") Column 1";
col2 = "(" + x + ") Column 2";
col3 = "(" + x + ") Column 3";
col4 = "(" + x + ") Column 4";

TableRow newRow = new TableRow(this);

TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
TextView column3 = new TextView(this);
TextView column4 = new TextView(this);

column1.setText(col1);

newRow.addView(column1);
newRow.addView(column2);
newRow.addView(column3);
newRow.addView(column4);

tl.addView(newRow, new TableLayout.LayoutParams());
}
}

}

最佳答案

您需要为每个列文本设置 setText

column1.setText(col1); 
column2.setText(col2);
column3.setText(col3);
column4.setText(col4);

关于android - 如何在表格布局中动态创建列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9511654/

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