2011-12-29 86 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。這更復雜,但如果您的收藏很大並且需要滾動瀏覽多個頁面,則效率可能會更高。