问题
I have a weird problem. I derived MessageRow from TableRow. I populated a table with one TableRow (Header) and 100 MessageRows. But when I add the table to the HorizontalScroll view only the Messagerows are showing. If I inspect the table in the debugger, all the rows are there, including the Header, with the right children and text.
This is the simplified code:
public class MessageRow extends TableRow {
public TextView tvData1;
public TextView tvData2;
public TextView tvData3;
public MessageRow(Context context) {
this(context, null);
}
public MessageRow(Context context, AttributeSet attrs) {
super(context, attrs);
tvData1 = new TextView(context);
tvData2 = new TextView(context);
tvData3 = new TextView(context);
}
public void setData(String data1, String data2, String data3) {
tvData1.setText(data1);
tvData2.setText(data2);
tvData3.setText(data3);
addView(tvData1);
addView(tvData2);
addView(tvData3);
}
}
The activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
mTable = new TableLayout(this);
HorizontalScrollView hview = (HorizontalScrollView) findViewById(R.id.hscroll);
mTable.setBackgroundColor(Color.WHITE);
populate(mTable);
hview.addView(mTable);
}
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
mHeader.addView(getColumnHeader("Data#1"));
mHeader.addView(getColumnHeader("Data#2"));
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
public void populate(TableLayout tl) {
setHeader(tl);
for(int i = 0; i < 100; i++) {
MessageRow mr = new MessageRow(getContext());
mr.setMessage("xxx"+i,"yyy"+i,"zzz"+i);
mr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mr);
}
}
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Now, I tried bypassing the getColumnHeader() function like this:
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
//mHeader.addView(getColumnHeader("Data#1"));
--> Button bt1 = new Button(getContext());
--> bt1.setText("Data#1");
--> mHeader.addView(bt1);
//mHeader.addView(getColumnHeader("Data#2"));
--> Button bt2 = getColumnHeader("Data#2");
--> mHeader.addView(bt2);
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
and then bt1 (Data#1) is displayed!!! but not Data#2 or Data#3. This hints that somehow the function getColumnHeader() is bad. But not acording to the debugger, when I inspect bt1 and bt2 they seem good, I cannot detect what is the difference between the manually instantiated button bt1 and the returned button bt2.
Side note: I don't know if this is the best way, but to compare the two variables I just right-clicked and Copy Variable, then pasted it to notepad++ in two different files (one for bt1 and one for bt2) and used the compare plugin.
回答1:
Some more info.
I managed to display the buttons, further debug showed that setting the Layout params for each button in the function getColumnHeader() caused the problem.
Still I don't know why.
The button member mLayoutParams being null is OK, but when created (.setLAyoutParams()) and assigned values (for example width and height) the suddenly the button is not displayed.
This is the patch,... until I figure out why.
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
//LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//params.rightMargin = params.leftMargin = 2;
//bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
回答2:
Last piece of the puzzle.
I tried many variations and finally it worked!
Replacing LayoutParams with TableRow.LayoutParams did the trick.
Their structure is very similar, probably even TableRow.LayoutParams is derived from LayoutParams.
I will leave the mystery here, until some expert sheds light on why this happened.
The final code:
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
来源:https://stackoverflow.com/questions/10259562/android-first-tablerow-is-not-showing