java - 动态添加的表行未出现

标签 java android tablelayout tablerow

我看过很多关于动态添加表格行的帖子,但我不确定我错过了什么。

当我执行以下命令时,没有显示任何内容(除了应用程序标题栏)。

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        android:id="@+id/table_view_test_main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        >
    <ScrollView
            android:id="@+id/tvt_scroll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            >
        <RelativeLayout
                android:id="@+id/tvt_scroll_relative"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <TableLayout
                    android:id="@+id/tvt_tableview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    >
            </TableLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

我的 Activity :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_view);

    TableLayout tableLayout = (TableLayout) findViewById(R.id.tvt_tableview);

    TableRow tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

    TextView column1 = new TextView(this);
    column1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    column1.setBackgroundColor(Color.YELLOW);
    column1.setTextColor(Color.BLUE);
    column1.setText("Col1 Value");
    tableRow.addView(column1);

    TextView column2 = new TextView(this);
    column2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    column2.setBackgroundColor(Color.RED);
    column2.setTextColor(Color.GREEN);
    column2.setText("Col2 Value");
    tableRow.addView(column2);

    tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

    //tl.refreshDrawableState();
    //findViewById(R.id.tvt_scroll_relative).refreshDrawableState();
}

最佳答案

改变两个引用

ViewGroup.LayoutParams

TableRow.LayoutParams

它应该可以工作。

向小部件添加布局参数要求 LayoutParams 是周围布局容器的内部类。

关于java - 动态添加的表行未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7541766/

相关文章:

java - 使用命令行执行java程序时maven报错

java - 在 Java 中用原语规避代码膨胀的模式

TableLayout 中的 Android EditText 跑掉了

java - 从自定义 slider 拖放不起作用

android - 如何关闭 textview 空白修剪?

Android TableRow RelativeLayout 问题

Android:在动态表格行的右侧动态添加图像

java - 是否可以在没有 Terracotta Enterprise Suite 的情况下使用 Ehcache 实现分布式缓存?

java - 幻影错误: cannot load SWT library;

android - 用于与 TI sensortag 配对的默认引脚是什么?