我有一个TableLayout
,我试图通过代码动态填写条目。在每个TableRow
中,我想以更复杂的方式显示内容,例如:我试图在{LinearLayout
内放置TableRow
1}},在这个布局中,我将嵌套其他LinearLayouts
以形成所需的结构。但是,在我的应用程序中它没有显示任何内容,所以我想知道是否有人可以查看我的代码并说出我在哪里出错了:
// Main layout, added to the TableRow
LinearLayout parentLayout = new LinearLayout(getContext());
parentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
parentLayout.setGravity(Gravity.CENTER_HORIZONTAL);
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
// Image at the start (left-side) of the row
ImageView iv = new ImageView(getContext());
iv.setBackgroundResource(R.drawable.img_10sec);
parentLayout.addView(iv);
// Layout for the two texts, which are on top of each other
// The layout should be of vertical orientation and be centred
// (for the sake of testing it, only 1 TextView is used)
LinearLayout group = new LinearLayout(getContext());
group.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
group.setGravity(Gravity.CENTER_VERTICAL);
group.setOrientation(LinearLayout.VERTICAL);
TextView name = new TextView(getContext());
name.setText("Example " + i);
name.setTextSize(20);
name.setSingleLine(false);
name.setPadding(10, 15, 0, 0);
group.addView(name);
// Tie everything together: table contains row, which contains the layout
parentLayout.addView(group);
row.addView(parentLayout);
table.addView(row);
如果我完全删除所有布局并坚持使用ImageView
&在TextView
内TableRow
,它运行正常。只有当我添加LinearLayouts
时,它才会中断,我无法理解原因。
答案 0 :(得分:1)
请尝试将layoutParameter
的{{1}}设为TableRow
和parentLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
希望这有帮助。