這個問題在Stack Overflow上有過解答了,解決方案還是很不錯的(前提是使用的 drawable 資源是矢量化的,對於位圖可能無效;關於矢量化圖像資源參見我的這篇文章)。
在資源文件的 android:drawble
屬性中寫入 dimen
中的量:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/icon"
android:width="@dimen/icon_size"
android:height="@dimen/icon_size"
/>
</layer-list >
但是注意,這種方法並不兼容 API21 以下,但也有解決方法,即在 build.gradle
文件中添加以下依賴:
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
問題源自 how to change a drawableLeft icon size on a button? 與 How to use vector drawables in Android API lower 21?。