繁体   English   中英

TableLayout中的ImageView

[英]ImageView In TableLayout

我想在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。 类型需要匹配布局,否则将无法正常工作。

尝试对行使用预定义的xml布局,就像有人在这里建议的那样: https : //stackoverflow.com/a/8679835/375929 (您可能不希望它完全动态,您的行很可能是相似的,您只需要以动态添加它们)将以编程方式节省LayoutParams的工作量。

但是,是的,您的问题可能是使用了错误的LayoutParams类,如matt5784所说。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM