【问题标题】:Table Rows created on the fly not showing up in the view [duplicate]动态创建的表行未显示在视图中[重复]
【发布时间】:2012-12-27 07:18:49
【问题描述】:

可能重复:
Programatically adding TableRow to TableLayout not working

代码如下:

DtoCbpOutcome[] outcomes = cbp.Outcomes;
Log.d(_logTag, "# of outcomes: " + Integer.toString(outcomes.length));
TableLayout tl = (TableLayout) findViewById(R.id.tbl_cbp_outcomes);
for (int i = 0; i < outcomes.length; i++) {

    // Text Views:
    // public String Description;

    TableRow tr = new TableRow(appCtx);
    tr.setLayoutParams(tbl_lay_parms);

    // Row Count
    TextView row_num = new TextView(appCtx);
    temp_string = Integer.toString(i + 1);
    row_num.setText(temp_string);
    row_num.setLayoutParams(lay_parms);
    tr.addView(row_num);

    // Description
    TextView desc = new TextView(appCtx);
    temp_string = outcomes[i].Description;
    desc.setText(temp_string);
    desc.setLayoutParams(lay_parms);
    tr.addView(desc);

    tl.addView(tr, tbl_lay_parms);
}

来自布局文件:

        <TableLayout
            android:id="@+id/tbl_cbp_outcomes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp" >
        </TableLayout>

我认为这是我正在构建的结构:

TableLayout (R.id.tbl_cbp_outcomes)
    TableRow (tr)
        TextView (row_num)
        TextView (desc)

但该行未显示在布局视图 (activity_business_plan.xml) 中。

我在这里做错了什么?

谢谢

更新 1

来自日志:结果数:5,由 outcomes.length 生成

更新 2

完整的布局xml:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_project_relationships"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tv_cbp_rel_global_priorities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="@string/double_dash" />
        <TextView
            android:id="@+id/tv_cbp_related_regional_priority_strategies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="@string/double_dash" />
        <TextView
            android:id="@+id/tv_cbp_related_whole_system_priorities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="@string/double_dash" />
        <TextView
            android:id="@+id/tv_cbp_related_demonstration_priorities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="@string/double_dash" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_vision_statement"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tv_cbp_vision_statement"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="@string/double_dash" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_outcomes"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TableLayout
            android:id="@+id/tbl_cbp_outcomes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp" >
        </TableLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_strategies"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TableLayout
            android:id="@+id/tbl_cbp_strategies"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp" >
        </TableLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_intermediate_results"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TableLayout
            android:id="@+id/tbl_cbp_intermediate_results"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp" >
        </TableLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_activities"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TableLayout
            android:id="@+id/tbl_cbp_activities"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp" >
        </TableLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_risks"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TableLayout
            android:id="@+id/tbl_cbp_risks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp" >
        </TableLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/green_nature_header"
            android:padding="3dp"
            android:text="@string/label_keywords"
            android:textColor="@color/White"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tv_cbp_keywords"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="@string/double_dash" />
        <TextView
            android:id="@+id/tv_cbp_modified"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="3dp"
            android:text="@string/double_dash"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 这看起来不错。发布包含您的表的 XML 以及 outcomes.length 是什么(超过 0)?
  • 我已经为表格添加了 xml。此外, results.length 产生 5。我手动检查了结果的数量。
  • 我想看看包含 TableLayout 的布局。
  • @Saifuddin 我检查了那个帖子,但结果并没有解决我的问题。
  • @Sam 我添加了 xml。谢谢。

标签: android


【解决方案1】:

我不知道是不是这样,但是:

你是否在tl.addView(tr, tbl_lay_parms);中使用“TableLayout.LayoutParams”;

tr.setLayoutParams(tbl_lay_parms) 中的“TableRow.LayoutParams”;

我觉得不一样,请查看http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout

最好的问候

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多