2011-12-29 55 views
2

在我的代码我需要创建一个表,每列包含位图和文本 - 我创建此表动态。 所以,我通过使用此代码这样做:如何控制将在ImageButton上绘制的位图大小?

for (int i = 0; i < collection.size(); i++) 
    {    

     ObjectITem item = collection.get(i); 

     LinearLayout linearLayout = new LinearLayout(this); 
     linearLayout.setOrientation(LinearLayout.HORIZONTAL); 

     TextView textView = new TextView(this);    
     textView.setText(item.getText());    

     linearLayout.addView(textView);    

     linearLayout.addView((new ImageButton(this)).setImageBitmap(item.getBitmap()); 
     layout.addView(linearLayout, LayoutParams.WRAP_CONTENT);   

    } 

这个代码工作不错 - 但因为渊源图像不相同大小 - 我看到上出现的ImageButton的图像看起来并不好。

  1. 如何让ImageButton上的所有图像看起来都一样?
  2. 有没有更好的方法来使我在这里做的布局?

回答

1

1)您可以使用Bitmap.createScaledBitmap()将所有图像缩放到所需的大小。像这样...

Bitmap buttonBmp = Bitmap.createScaledBitmap(item.getBitmap(), buttonW, buttonH, true); 
linearLayout.addView((new ImageButton(this)).setImageBitmap(buttonBmp); 

2)你可能要考虑使用ListActivityListView。这更复杂,但如果您的收藏很大并且需要滚动浏览多个页面,则效率可能会更高。