不显示动态添加表行 [英] Dynamically added table rows not appearing

查看:244
本文介绍了不显示动态添加表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所看到的关于动态添加表行很多帖子,但我不知道我错过了什么。

当我执行以下,不会显示任何内容(除了应用程序标题栏)。

我的布局:

 <?XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
        机器人:ID =@ + ID / table_view_test_main
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        >
    <滚动型
            机器人:ID =@ + ID / tvt_scroll
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_alignParentTop =真
            机器人:layout_alignParentLeft =真
            >
        < RelativeLayout的
                机器人:ID =@ + ID / tvt_scroll_relative
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT>
            < TableLayout
                    机器人:ID =@ + ID / tvt_tableview
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =WRAP_CONTENT
                    >
            < / TableLayout>
        < / RelativeLayout的>
    < /滚动型>
< / RelativeLayout的>

我的活动:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.table_view);    TableLayout tableLayout =(TableLayout)findViewById(R.id.tvt_tableview);    的TableRow的TableRow =新的TableRow(本);
    tableRow.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));    TextView中列1 =新的TextView(本);
    column1.setLayoutParams(新ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    column1.setBackgroundColor(Color.YELLOW);
    column1.setTextColor(Color.BLUE);
    column1.setText(COL1值);
    tableRow.addView(列1);    TextView中列2 =新的TextView(本);
    column2.setLayoutParams(新ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    column2.setBackgroundColor(Color.RED);
    column2.setTextColor(Color.GREEN);
    column2.setText(col2的值);
    tableRow.addView(列2);    tableLayout.addView(tablerow的,新的TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));    //tl.refreshDrawableState();
    // findViewById(R.id.tvt_scroll_relative).refreshDrawableState();
}


解决方案

更改两个引用

  ViewGroup.LayoutParams

TableRow.LayoutParams

和它应该工作。

添加布局PARAMS到窗口小部件要求的LayoutParams 是一个内部类的周边布局的容器。

I have seen many posts regarding dynamically adding table rows, but I am not sure what I'm missing.

When I execute the following, nothing is displayed (aside from application title bar).

My Layout:

<?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>

My 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();
}

解决方案

Change the two references

ViewGroup.LayoutParams

to

TableRow.LayoutParams

and it should work.

Adding layout params to a widget requires that the LayoutParams are a inner class of the surrounding layout container.

这篇关于不显示动态添加表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆