2012-04-11 47 views
2

問題是:我有一個Imageview,我想在圖像視圖周圍創建一個2dp寬的白色邊框/填充。 imageview在java中被刪除,我想在java中執行填充,而不是xml。Android Padding Color

+0

也許你可以適應這段代碼:http://stackoverflow.com/questions/6957032/android-padding-left-a-bitmap-with-white-color – TryTryAgain 2012-04-11 19:47:50

回答

3

ImageView支持2種東西:前景中的背景和位圖。兩者都可以從XML中設置爲Drawables,Bitmaps或Resources。所以在Java中

,你應該能夠做到這一點:

ImageView view = new ImageView(this); 
view.setImageResource(R.drawable.splash); // Adds the foreground Bitmap 
view.setScaleType(ScaleType.CENTER_INSIDE); // Sets how the bitmap is scaled in it's container 
view.setBackgroundColor(Color.WHITE);  // Define the border color 
view.setPadding(2,2,2,2);     // Define the border size 
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

這應該做的伎倆。