java - 以编程方式设置可绘制大小

标签 java android

图像(图标)的大小大致相同,但我需要调整它们的大小以使按钮保持相同的高度。

我该怎么做?

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);

最佳答案

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"

关于java - 以编程方式设置可绘制大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4609456/

相关文章:

java - 在vaadin项目中集成小程序时出现ClassFormatException

android - Gradle 任务不在 Android Studio 中执行

android - Honeycomb webview 上的网页不可用

java - 从退回邮件中读取附件名称

java - 没有重复的集合(基于 id)但保留最高版本号

android - 我在 Android 中使用新的 Places SDK。 AutocompleteSupportFragment,它要求我将 Activity 添加到 list 中

android - Interceptor.Chain.proceed() 是线程阻塞方法吗?

android - 如何在android上制作准确的指南针

java - 在软键盘 android 上收听每个按键事件

Java keystore 维护实用程序