如何使按钮上的可绘制对象更小?图标太大了,居然比按钮高。这是我正在使用的代码:
<Button
android:background="@drawable/red_button"
android:drawableLeft="@drawable/s_vit"
android:id="@+id/ButtonTest"
android:gravity="left|center_vertical"
android:text="S-SERIES CALCULATOR"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:drawablePadding="10dp">
</Button>
上面是它应该看起来的样子,下面是它现在看起来的样子。
我试过了,但没有显示图像。 :-(
Resources res = getResources();
ScaleDrawable sd = new ScaleDrawable(res.getDrawable(R.drawable.s_vit), 0, 10f, 10f);
Button btn = (Button) findViewById(R.id.ButtonTest);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null);
最佳答案
我找到了一个非常简单有效的 XML 解决方案,不需要 ImageButton
为您的图像制作如下可绘制文件并将其用于 android:drawableLeft
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/half_overlay"
android:drawable="@drawable/myDrawable"
android:width="40dp"
android:height="40dp"
/>
</layer-list>
您可以使用 android:width
和 android:height
属性设置图像大小。
这样你至少可以为不同的屏幕获得相同的尺寸。
缺点是它不完全像 fitXY 那样缩放图像宽度以适应 X 并相应地缩放图像高度。
关于android - 如何缩小按钮上的可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7538021/