android - 如何缩小按钮上的可绘制对象?

标签 android scale compound-drawables

如何使按钮上的可绘制对象更小?图标太大了,居然比按钮高。这是我正在使用的代码:

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

上面是它应该看起来的样子,下面是它现在看起来的样子。

The upper is how it should look, the lower how it looks right now.

我试过了,但没有显示图像。 :-(

    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:widthandroid:height 属性设置图像大小。

这样你至少可以为不同的屏幕获得相同的尺寸。

缺点是它不完全像 fitXY 那样缩放图像宽度以适应 X 并相应地缩放图像高度。

关于android - 如何缩小按钮上的可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7538021/

相关文章:

r - Alpha并在ggplot2箱图中填充图例?

android - CompoundButton Checkbox 在哪里/如何测量可绘制复选框的宽度

java - 为什么 minifyEnabled 显示我的大部分代码?

android - 在 android 的 Ionic 项目中的位置

Android:在翻译中缩放 EditText Android 问题并在放置在 parentView 外部时获取 childView 的 Touch 事件

android - 在 TextView 上绘制背景(不是全宽)

android - 在事件上向 EditText 显示复合可绘制对象

android - 警告 : This AsyncTask class should be static or leaks might occur

android - 列类型 INTEGER 的 SQLite 查询 WHERE 子句

Matlab:如何在等高线图上设置对数刻度