【发布时间】:2017-05-26 12:24:18
【问题描述】:
在下面的代码 sn-p 中有一个注释行。当我取消注释该行时,LinearLayout 的内容不会显示在TableRow 中。如果不设置LayoutParams,该行会显示两个文本。我不明白这种行为。我知道我可以通过 xml 文件包含复杂的视图,但我更想了解这段代码有什么问题:
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
TableRow tableRow = new TableRow(this );
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// when I comment out this line, the row only shows the second text.
// linearLayout.setLayoutParams(layoutParams);
TextView textLabel = new TextView(this);
textLabel.setText("inside linear layout");
linearLayout.addView(textLabel);
TextView message = new TextView(this);
message.setText( "inside tablerow");
tableRow.addView(linearLayout);
tableRow.addView(message);
tableLayout.addView(tableRow);
【问题讨论】:
-
也许您忘记为文本视图添加布局参数?我认为现在 TextView 的宽度和高度为 0dp,而 linearLayout 具有 match_parent 参数,因此您看到的是空视图
-
当我使用
textLabel.setHeight(20); textLabel.setWidth(20)时它也不显示 -
尝试更改linearLayout的layoutParams源 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
-
感谢您的提示。我现在尝试使用
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);,但它仍然没有出现。
标签: android android-layout android-view android-tablelayout android-layoutparams