3 回答
![?](http://img1.sycdn.imooc.com/545863cd0001b72a02200220-100-100.jpg)
TA贡献1804条经验 获得超3个赞
您可以使用该setCompoundDrawables方法来执行此操作。请参阅此处的示例。我使用了此功能,但未使用,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);
![?](http://img1.sycdn.imooc.com/5458657e000125a302200220-100-100.jpg)
TA贡献1831条经验 获得超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);
![?](http://img1.sycdn.imooc.com/533e51f30001edf702000200-100-100.jpg)
TA贡献1847条经验 获得超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)
}
}
添加回答
举报