【问题标题】:TableRow is being added but doesn't display正在添加 TableRow 但不显示
【发布时间】:2012-05-06 15:35:01
【问题描述】:

我有一个小应用程序需要定期将表格行添加到表格布局中。根据 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="*">

【问题讨论】:

  • 你能在你有父ScrollViewTableLayout的地方添加布局文件吗?

标签: android android-layout


【解决方案1】:

您是否运行了在TableLayout 中添加一行的代码?我模拟了单击Button 时添加一行,但我得到了一个尴尬的Divide by 0 异常。

无论如何,将封闭的LinearlayoutLayoutParams 更改为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)

另外,如果这不能解决问题,请尝试添加:

android:fillViewport="true"

到 xml 布局中的 ScrollView 标记。

【讨论】:

  • 托皮卡!这在没有实现 fillviewport 的情况下工作。但是,滚动尚未开始,因此在添加 x 行后我可能需要它。有时,当我有一个零行的表时,我会在编辑器中得到除以零的错误,但会因为有点烦躁而消失。非常感谢我已经修改了几个小时
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
相关资源
最近更新 更多