2011-02-26 59 views
14

我有一个ImageButton的,我使用的是包括@android:绘制/ ic_menu_more图像。但它太大了。调整大小以适应我的表单更好的最佳方式是什么?另外,它可以在xml中旋转吗? (只有一次,而不是基于状态)调整大小的ImageButton?

回答

31

尝试上的按钮设置图像设置android:background而不是android:src。这可能有助于你的情况,因为它会将图像拉伸到按钮的大小。此外,你将不得不指定按钮(使用dip而不是px)固定尺寸。例如:

<ImageButton 
    android:background="@drawable/ic_menu_more" 
    android:layout_width="50dip" 
    android:layout_height="50dip" /> 
+2

对于那些以编程方式进行编程的人,请使用setbackgrounddrawable或setbackground代替setimageresource – 2013-07-09 15:35:29

2

从史蒂芬 - byle的解决方案(https://stackoverflow.com/a/15117536/1741997),他说:

” ...使用的是Android:scaleType = “fitCenter” 有Android的缩放图像,和android:adjustViewBounds = “真”,让他们调整,由于其缩放范围......”

它为我

0

我解决了这个问题是从有问题的绘图资源创建位图,然后把它转换成一个方式缩放位图与所需的大小。我也将背景设置为透明以仅显示图像。

int height = 300; 
    int width = 500 
    Bitmap bmp; 
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.myresourcename); 
    bmp = Bitmap.createScaledBitmap(bmp, width, height, true); 
    ImageButton imageButton = new ImageButton(this.getActivity()); 
    imageButton.setImageBitmap(bmp); 
    imageButton.setBackgroundColor(Color.TRANSPARENT); 

然后将其添加到布局。

相关问题