android-通过sql循环将多个TextView动态添加到TableRow
作者:互联网
我在这里搜索过,但似乎无法解决此问题.我有一个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 aTableLayout
. If aTableRow
‘s parent is not aTableLayout
, theTableRow
will behave as an horizontalLinearLayout
.
如果您只是想让每对TextView共享公共背景R.drawable.shape_border,则可以使用嵌套的LinearLayout代替TableRow(无论如何,TableRow是从LinearLayout扩展的).
或者,如果您确实要使用TableRow的某些特定功能,则使R.id.linearCores为TableLayout而不是LinearLayout.
标签:tablerow,android-linearlayout,android,textview 来源: https://codeday.me/bug/20191119/2034579.html