图像(图标)的大小大致相同,但我需要调整它们的大小,以使按钮保持相同的高度.
我该怎么做呢?
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);
Run Code Online (Sandbox Code Playgroud)
Eri*_*hen 144
该setBounds()方法不适用于每种类型的容器(但对我ImageView的某些容器起作用).
尝试以下方法来缩放drawable本身:
// 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"
Run Code Online (Sandbox Code Playgroud)
jkh*_*uw1 31
指定尺寸setBounds(),即使用50x50尺寸
drawable.setBounds(0, 0, 50, 50);
Run Code Online (Sandbox Code Playgroud)
public void setBounds(int left,int top,int right,int bottom)
iBo*_*Bog 14
在应用之前.setBounds(..)尝试将当前的Drawable转换为ScaleDrawable
drawable = new ScaleDrawable(drawable, 0, width, height).getDrawable();
Run Code Online (Sandbox Code Playgroud)
之后
drawable.setBounds(0, 0, width, height);
Run Code Online (Sandbox Code Playgroud)
将工作
我没有时间去挖掘为什么setBounds()方法没有像预期的那样处理位图drawable但是我很少调整@androbean-studio解决方案来做setBounds应该做的事情......
/**
* Created by ceph3us on 23.05.17.
* file belong to pl.ceph3us.base.android.drawables
* this class wraps drawable and forwards draw canvas
* on it wrapped instance by using its defined bounds
*/
public class WrappedDrawable extends Drawable {
private final Drawable _drawable;
protected Drawable getDrawable() {
return _drawable;
}
public WrappedDrawable(Drawable drawable) {
super();
_drawable = drawable;
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
//update bounds to get correctly
super.setBounds(left, top, right, bottom);
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.setBounds(left, top, right, bottom);
}
}
@Override
public void setAlpha(int alpha) {
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.setAlpha(alpha);
}
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.setColorFilter(colorFilter);
}
}
@Override
public int getOpacity() {
Drawable drawable = getDrawable();
return drawable != null
? drawable.getOpacity()
: PixelFormat.UNKNOWN;
}
@Override
public void draw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.draw(canvas);
}
}
@Override
public int getIntrinsicWidth() {
Drawable drawable = getDrawable();
return drawable != null
? drawable.getBounds().width()
: 0;
}
@Override
public int getIntrinsicHeight() {
Drawable drawable = getDrawable();
return drawable != null ?
drawable.getBounds().height()
: 0;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
// get huge drawable
final Drawable drawable = resources.getDrawable(R.drawable.g_logo);
// create our wrapper
WrappedDrawable wrappedDrawable = new WrappedDrawable(drawable);
// set bounds on wrapper
wrappedDrawable.setBounds(0,0,32,32);
// use wrapped drawable
Button.setCompoundDrawablesWithIntrinsicBounds(wrappedDrawable ,null, null, null);
Run Code Online (Sandbox Code Playgroud)
结果
使用
textView.setCompoundDrawablesWithIntrinsicBounds()
Run Code Online (Sandbox Code Playgroud)
你的minSdkVersion应该是build.gradle中的17
defaultConfig {
applicationId "com.example..."
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Run Code Online (Sandbox Code Playgroud)
要更改可绘制的大小:
TextView v = (TextView)findViewById(email);
Drawable dr = getResources().getDrawable(R.drawable.signup_mail);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 80, 80, true));
//setCompoundDrawablesWithIntrinsicBounds (image to left, top, right, bottom)
v.setCompoundDrawablesWithIntrinsicBounds(d,null,null,null);
Run Code Online (Sandbox Code Playgroud)
小智 -47
Button button = new Button(this);
Button = (Button) findViewById(R.id.button01);
Run Code Online (Sandbox Code Playgroud)
使用Button.setHeight()或Button.setWeight()并设置一个值。