ImageView宽度和高度以编程方式在TableLayout中

时间:2012-07-03 19:29:38

标签: java android

其实我想要的只是问题标题。我想在表格布局中为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);
    tableRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


    ImageView imageView = new ImageView(this);
    imageView.setImageResource(R.drawable.troll_logo);
    //imageView.setLayoutParams(new TableLayout.LayoutParams(100, 100));

    tableRow.addView(imageView);
    buddy_list_layout.addView(tableRow);
} // end -> method -> onCreate

1 个答案:

答案 0 :(得分:2)

尝试:

    ImageView imageView  = new ImageView(this);
    LinearLayout.LayoutParams vp = 
        new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                        LayoutParams.WRAP_CONTENT);
    imageView.setLayoutParams(vp); 
    imageView.getLayoutParams().height = 20;
    imageView.getLayoutParams().width  = 20;
    imageView.setImageResource(R.drawable.troll_logo);
////.......