3 回答

TA貢獻1804條經(jīng)驗 獲得超3個贊
您可以使用該setCompoundDrawables方法來執(zhí)行此操作。請參閱此處的示例。我使用了此功能,但未使用,setBounds并且效果良好。您可以嘗試任何一種方式。
更新:如果鏈接斷開,請在此處復制代碼
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );
要么
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
要么
txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);

TA貢獻1831條經(jīng)驗 獲得超9個贊
我這樣做:
// Left, top, right, bottom drawables.
Drawable[] drawables = button.getCompoundDrawables();
// get left drawable.
Drawable leftCompoundDrawable = drawables[0];
// get new drawable.
Drawable img = getContext().getResources().getDrawable(R.drawable.ic_launcher);
// set image size (don't change the size values)
img.setBounds(leftCompoundDrawable.getBounds());
// set new drawable
button.setCompoundDrawables(img, null, null, null);

TA貢獻1847條經(jīng)驗 獲得超7個贊
Kotlin Version
使用以下代碼片段向按鈕添加一個可繪制對象:
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_favorite_white_16dp)
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
。
? Important Point in Using Android Vector Drawable
當您使用可繪制的android vector并希望向21以下的API向后兼容時,請將以下代碼添加到:
在應用程序級別build.gradle:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
在應用程序類中:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}
}
添加回答
舉報