在下面的代碼片段中有一條註釋行。當我取消註釋該行時,則LinearLayout
的內容不會顯示在TableRow
中。如果不設置LayoutParams
,該行將顯示兩個文本。我不明白這種行爲。我知道,我可以通過xml
文件包括複雜的看法,但我寧願明白什麼是錯與此代碼:應用LayoutParams隱藏視圖
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);
也許你忘記添加布局params用於在文本視圖?我認爲TextView的寬度和高度現在是0dp,並且linearLayout具有match_parent參數,因此您可以看到空視圖 –
當我使用'textLabel.setHeight(20); textLabel.setWidth(20)'它也沒有顯示 – shaft
嘗試更改linearLayout的layoutParams源 新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); –