1

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

1 回答 1

2

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

无论如何,LayoutParams 将封闭的更改LinearlayoutTableRow.LayoutParams(其父级是TableRow):

    TableRow.LayoutParams lp = new TableRow.LayoutParams(
            TableRow.LayoutParams.FILL_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
    ll.setId(1589);
    ll.setLayoutParams(lp)

Also, if this doesn't solve the problem, try to add:

android:fillViewport="true"

to the ScrollView tag in the xml layout.

于 2012-05-06T07:35:01.000 回答