0

我有以下内容LinearLayout,我想在其中插入一些动态生成TableLayout的 s。这运行没有任何错误,但屏幕上没有出现任何内容。为什么TableLayouts没有出现?如何生成TableLayouts 并将它们添加到 s 中LinearLayout

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/lblOverviewText">  
</LinearLayout>

这就是我生成TableLayouts 的方式:

var linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
foreach (var block in status.blocks)
{

    var tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FillParent, TableLayout.LayoutParams.FillParent);
    var rowParams = new TableLayout.LayoutParams(TableRow.LayoutParams.FillParent, TableRow.LayoutParams.WrapContent);

    var tableLayout = new TableLayout(this);
    tableLayout.LayoutParameters = tableParams;


    TableRow tableRow = new TableRow(this);
    tableRow.LayoutParameters =tableParams;

    TextView textView = new TextView(this);
    textView.Text = block.Name;
    textView.LayoutParameters = rowParams;

    tableRow.AddView(textView);

    tableLayout.AddView(tableRow, rowParams);
    linearLayout.AddView(tableLayout);
}
4

1 回答 1

0

首先,我将删除线性布局的 xml 中的 alignParentLeft/alignParentRight,然后简单地放置 android:layout_width="match_parent"。您还需要在 xml 中将线性布局的“方向”属性定义为“垂直”。

问题的下一部分是不同类型的布局参数的复杂组合,您已经正确地确定了对表格和行布局参数的需求,但是已经将 rowParams 变量定义为“新 TableLayout.LayoutParams”,而不是它应该是'new TableRow.LayoutParams' 但两者都应具有 match_parent 的宽度和 wrap_content 的高度。下面的代码示例:

  LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2);
    for(int i = 0; i < listItems.length; i++)
    {
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);

        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setLayoutParams(lp2);
        tableLayout.setColumnStretchable(0, true);//NOTE: you may not want this if you do not want your textview to always fill the available space

        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(lp);

        TextView textView = new TextView(this);
        textView.setText(listItems[i]);
        textView.setLayoutParams(lp);
        tableRow.addView(textView);

        tableLayout.addView(tableRow);
        linearLayout.addView(tableLayout);
    }

希望这可以帮助。

于 2013-08-09T15:39:16.470 回答