【问题标题】:Views dynamically added to TableLayout not showing动态添加到 TableLayout 的视图未显示
【发布时间】:2013-12-07 19:15:13
【问题描述】:

我在 xml 中有一个 TableLayout,我想以编程方式向其中添加行。

这是我的代码:

LinearLayout deckBuilder = (LinearLayout) getLayoutInflater().inflate(R.layout.deckbuilder, null);

TableLayout deckGrid = new TableLayout(this.getApplicationContext());

    int rows = getResources().getInteger(R.integer.deckbuilder_grid_rows);
    int columns = getResources().getInteger(R.integer.deckbuilder_grid_columns);

    deckGrid.setWeightSum(rows);

    for (int i = 0; i < rows; ++i) {
        TableRow row = new TableRow(this.getApplicationContext());
        for (int j = 0; j < columns; ++j) {
            ImageView iv = new ImageView(this.getApplicationContext());
            row.addView(iv);
            iv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1));
            iv.setImageResource(R.drawable.my_image);
        }
        deckGrid.addView(row);
        row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1));
    }
    deckBuilder.addView(deckGrid);
    deckGrid.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

问题是我在行中添加的视图没有显示,行本身也没有显示。

行:3,列:5。

这里是deckbuilder的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/deckbuilder_background"
    android:orientation="vertical" >
</LinearLayout>

【问题讨论】:

  • 您好,首先,您不应该使用 applicationCONtext 创建 UI 组件,其次您是否打印了行和列的值,如果是,请告诉我们数字,这样会容易得多如果您要在此处添加用于 deckbuilder 的 xml,我们可以为您提供帮助

标签: android dynamic android-tablelayout


【解决方案1】:

我想你的代码在一个扩展 Activity 的类中。

所以你应该放

setContentView(deckBuilder);

在最后一行之后显示LinearLayout

此外,在Activity 中,您可以直接使用this 代替this.getApplicationContext()

【讨论】:

  • setContentView(deckBuilder) 在课程开始时完成。我尝试使用 'this' 而不是 this.getApplicationContext(),但它仍然不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 2013-03-15
  • 1970-01-01
相关资源
最近更新 更多