调用setCompoundDrawables()方法不能显示复合图标

373

我调用了setCompoundDrawables方法后,合成图标没有显示。

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);

有什么想法吗?


12
正如下面的答案所述,需要调用名为“(…)WithIntrinsicBounds”的方法变体。顺便提一句,在调用此方法后必须设置 Compound Drawable 的 padding 才能产生效果。 - Dr1Ku
8
这篇文档的意思是:这些Drawable对象必须先调用过 setBounds(Rect)方法。 - user1521536
嗨,hunterp,我在咖啡店(Angel)遇见了你,现在知道你知道什么是Android Drawables了(也许你在使用很多时遇到了内存不足的错误),我可以告诉你一些我合作过的项目如何处理这个问题,去看看https://github.com/JakeWharton/DiskLruCache(我协助使其更加适合Android),它被Picasso(https://github.com/square/picasso)所使用。 - Gubatron
1
@Dr1Ku 实际上我以前就有它,而且它仍然有效。 - Sotti
请查看此链接 https://dev59.com/dm445IYBdhLWcg3wg6tm#71966649 - Mori
11个回答

1
在使用Kotlin和Databinding时。
binding.tvTime.setCompoundDrawablesWithIntrinsicBounds(
            ResourcesCompat.getDrawable(resources, android.R.drawable.ic_menu_recent_history, null),
            null,null,null)

这段代码涉及到IT技术,其中tvTime代表文本视图,而位置则为(右、上、左、下)。更多关于绑定(binding)的信息可以在这里找到。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接