由网友(少年深思)分享简介:的图像(图标)来在大致相同的大小,但我需要调整它们的大小,以使按钮保持相同的高度。我如何做到这一点?按钮按钮=新的按钮(这一点);button.setText(apiEventObject.getTitle());button.setOnClickListener(听众);/ **设置按钮,点击ID实际事件ID*...
的图像(图标)来在大致相同的大小,但我需要调整它们的大小,以使按钮保持相同的高度。
我如何做到这一点?
按钮按钮=新的按钮(这一点);
button.setText(apiEventObject.getTitle());
button.setOnClickListener(听众);
/ *
*设置按钮,点击ID实际事件ID
* /
INT的id =的Integer.parseInt(apiEventObject.getId());
button.setId(ID);
button.setLayoutParams(新的LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
可绘制绘制= LoadImageFromWebOperations(apiSizeObject.getSmall());
//?调整绘制在这里? drawable.setBounds(50,50,50,50);
button.setCompoundDrawablesWithIntrinsicBounds(绘制,NULL,NULL,NULL);
解决方案
在的setBounds()
方法不适用于所有类型的容器中工作(没有工作的一些我的的ImageView
的,不过)。
试试下面的方法来缩放绘制本身:
//读取你绘制从某处
。可绘制博士= getResources()getDrawable(R.drawable.somedrawable);
点阵位图=((BitmapDrawable)博士).getBitmap();
//缩放到50×50
绘制对象D =新BitmapDrawable(getResources(),Bitmap.createScaledBitmap(位图,50,50,真));
//设置新的,按比例绘制的D
The images (icons) come in roughly the same size, but I need to resize them in order for the buttons to remain the same height.
How do I do this?
Button button = new Button(this);
button.setText(apiEventObject.getTitle());
button.setOnClickListener(listener);
/*
* set clickable id of button to actual event id
*/
int id = Integer.parseInt(apiEventObject.getId());
button.setId(id);
button.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
Drawable drawable = LoadImageFromWebOperations(apiSizeObject.getSmall());
//?resize drawable here? drawable.setBounds(50, 50, 50, 50);
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
解决方案
The setBounds()
method doesn't work for every type of container (did work for some of my ImageView
's, however).
Try the method below to scale the drawable itself:
// Read your drawable from somewhere
Drawable dr = getResources().getDrawable(R.drawable.somedrawable);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// Scale it to 50 x 50
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));
// Set your new, scaled drawable "d"
相关推荐
最新文章