我有一个小应用程序,需要定期将表格行添加到tablelayout。根据getChildCount()方法,正在添加行但不显示该行。任何建议将不胜感激
TableLayout tbl = (TableLayout)findViewById(R.id.mytbl);
TableRow row = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
TextView txtView = new TextView(this);
row.setId(1234);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER);
row.setBackgroundResource(android.R.color.darker_gray);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
ll.setId(1589);
ll.setLayoutParams(lp);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundResource(R.color.header_background);
ll.setGravity(Gravity.CENTER);
String msg = "Sample Text";
txtView.setId(657);
txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
txtView.setTextColor(R.color.a_color);
txtView.setBackgroundResource(R.drawable.border_a);
txtView.setGravity(Gravity.TOP | Gravity.LEFT);
txtView.setText(msg);
ll.addView(txtView);
row.addView(ll);
tbl.addView(row,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));//maybe add this with layout params tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
我已尝试过tbl.RequestLayout()和tbl.Invalidate()但没有帮助。该表包含在一个滚动视图中,这是我最好的猜测问题所在。我几个小时一直在修补这个问题,所以在正确的方向上轻推确定会有所帮助
<ScrollView
android:id="@+id/scrVw_lst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:id="@+id/mytbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
答案 0 :(得分:2)
您是否运行了在TableLayout
中添加行的代码?我已经模拟了在点击Button
时添加一行,我得到了一个尴尬的Divide by 0
例外。
无论如何,将封闭式LayoutParams
的{{1}}更改为Linearlayout
(其父级为TableRow.LayoutParams
):
TableRow
此外,如果这不能解决问题,请尝试添加:
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
ll.setId(1589);
ll.setLayoutParams(lp)
到xml布局中的android:fillViewport="true"
标记。