Android 正在添加TableRow,但未添加';t显示
我有一个小应用程序,它定期需要在tablelayout中添加一个表行。根据getChildCount()方法,正在添加行,但不显示该行。如有任何建议,将不胜感激Android 正在添加TableRow,但未添加';t显示,android,android-layout,Android,Android Layout,我有一个小应用程序,它定期需要在tablelayout中添加一个表行。根据getChildCount()方法,正在添加行,但不显示该行。如有任何建议,将不胜感激 TableLayout tbl = (TableLayout)findViewById(R.id.mytbl); TableRow row = new TableRow(this); LinearLayout ll = new LinearLayout(this); TextView txtView =
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的异常
无论如何,请将封闭的Linearlayout
的LayoutParams
更改为TableRow.LayoutParams
(其父级为TableRow
):
此外,如果这不能解决问题,请尝试添加:
android:fillViewport="true"
到xml布局中的ScrollView
标记。是否可以添加布局文件,其中包含父ScrollView
和TableLayout
?Topeka!这在没有实现fillviewport的情况下有效。但是,滚动还没有开始,所以我可能需要在添加x行之后使用它。有时,当我有一个表的行数为零时,我会在编辑器中得到被零除的错误,但会有点坐立不安。非常感谢,我已经修补了好几个小时了
android:fillViewport="true"