通过 sql 循环将多个 TextView 动态添加到 TableRow

Add multiple TextView to a TableRow dynamically via sql loop

我在这里找了又找,但似乎无法解决这个问题。我有一个 ScrollView,在 ScrollView 里面是一个 LinearLayout,我想读取我的 SQL 数据库并显示这样的结果;

Linear Layout
    ScrollView
        Linear Layout
            TableRow
               TextView
               TextView
            TableRow
               TextView
               TextView
        /Linear Layout
    /ScrollView
/LinearLayout

我的代码如下:

TableRow tRow;
            ContextThemeWrapper ttRow = new ContextThemeWrapper(this, R.style.coreTable);
            LinearLayout LL = (LinearLayout) findViewById(R.id.linearCores);
            LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);



            if (cores.moveToFirst()) {
                while (cores.isAfterLast() == false) {
                    Log.e("CORE LIST", cores.getString(1));
                    tRow = new TableRow(ttRow);
                    tRow.setLayoutParams(lp);
                    tRow.setOrientation(TableRow.VERTICAL);
                    tRow.setId(cores.getInt(0));
                    tRow.setBackgroundResource(R.drawable.shape_border);
                    ContextThemeWrapper newTxtA = new ContextThemeWrapper(this, R.style.coreHeaderView);
                    TextView tTextA = new TextView(newTxtA);
                    tTextA.setLayoutParams(lp);
                    tTextA.setText(cores.getString(1) + " (Lvl " + cores.getString(2) + ")");
                    tRow.addView(tTextA);
                    TextView tTextB = new TextView(coreChooser.this);
                    tTextB.setLayoutParams(lp);
                    tTextB.setText(cores.getString(5));
                    tRow.addView(tTextB);
                    LL.addView(tRow);
                    cores.moveToNext();
                }
            }

在我的模拟器上它显示了第一个 tRow.addView,但没有显示其余部分,但是背景似乎延伸到了屏幕之外。

我真的不确定我做错了什么。

TableRow 的文档说明如下:

A TableRow should always be used as a child of a TableLayout. If a TableRow's parent is not a TableLayout, the TableRow will behave as an horizontal LinearLayout.

如果您的目的只是让每对 TextView 共享共同的背景 R.drawable.shape_border,则使用嵌套的 LinearLayout 代替 TableRowTableRow 是从 LinearLayout 扩展而来的)。

或者,如果您绝对想使用 TableRow 的某些特定功能,则将 R.id.linearCores 设为 TableLayout 而不是 LinearLayout