我有以下LinearLayout
,我想在其中插入一些动态生成的TableLayout
。这样运行没有任何错误,但屏幕上没有任何内容。为什么不出现TableLayout
?如何生成TableLayout
并将其添加到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>
这就是我生成TableLayout
s的方式:
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);
}
答案 0 :(得分:0)
首先,我将删除xml中为linearlayout的alignParentLeft / alignParentRight,并简单地放入android:layout_width =“match_parent”。 您还需要将xml中linearlayout的'orientation'属性定义为'vertical'。
问题的下一部分是不同类型的布局参数的复杂混合,你已经正确地确定了表和行布局参数seperatley的需要但是已经将rowParams变量定义为'new 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);
}
希望这有帮助。