android – 如何在tablelayout中动态创建列?

我想了解如何动态地将列和行添加到tablelayout中.

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

有人可以告诉我,为了显示四列而不是一列吗?

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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...