我想在 TableLayout 中添加一个 ImageView。我做了所有事情,但没有显示任何内容。我该如何解决?
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buddy_list);
TableLayout buddy_list_layout = (TableLayout)findViewById(R.id.buddy_list_layout);
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams layoutParams1 = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
50);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.troll_logo);
TableLayout.LayoutParams layoutParams2 = new TableLayout.LayoutParams(50, 50);
tableRow.addView(imageView, layoutParams2);
buddy_list_layout.addView(tableRow, layoutParams1);
} // end -> method -> onCreate
最佳答案
您应该将 layoutParams2 从 TableLayout.LayoutParams()
切换到 TableRow.LayoutParams()
,因为您要使用它来添加 TableRow。类型需要与布局相匹配,否则将无法工作。
关于java - TableLayout 中的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11315088/