以编程方式设置宽度Imageview

时间:2011-10-04 09:57:21

标签: android height width set imageview

我已经尝试了很多方法,但似乎不适用于我。

我的代码: TableLayout tTopic=(TableLayout) findViewById(R.id.__topic);

    TableRow tr = new TableRow(this);
    TableLayout.LayoutParams lpt2=null;
    lpt2 = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT);
    lpt2.setMargins(4, 2, 4, 2);

    tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    ImageView view = new ImageView(this);
    view.setLayoutParams(new LayoutParams(20,30));

    view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    view.setImageResource(R.drawable.kotak);


    tr.addView(view);
    tTopic.addView(tr,lpt2);

TableRow tr = new TableRow(this); TableLayout.LayoutParams lpt2=null; lpt2 = new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT); lpt2.setMargins(4, 2, 4, 2); tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); ImageView view = new ImageView(this); view.setLayoutParams(new LayoutParams(20,30)); view.setScaleType(ImageView.ScaleType.CENTER_INSIDE); view.setImageResource(R.drawable.kotak); tr.addView(view); tTopic.addView(tr,lpt2); 为什么它根本没有出现?但是当我省略setLayoutParams时,它显示为默认大小。谢谢你的帮助

2 个答案:

答案 0 :(得分:4)

试试这个:

view.setLayoutParams(new TableRow.LayoutParams(20,30));

答案 1 :(得分:1)

以编程方式设置ImageView宽度:

view.getLayoutParams().width = 20;