TextView 等setCompoundDrawables 无效的解决办法
Android setCompoundDrawables 无效的原因和解决办法
今天通过一个TextView这样一个效果,通过代码动态切换drawableRight的箭头图片
问题描述
需要在代码中动态切换切换drawableRight的图片,通过setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
时发现没有设置上图片资源代码如下:
原因分析
setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)方法可以根据需求为Teview等控件设置对应位置的图片资源,但是设置的Drawable
必须已经setBounds(Rect)
,即你要添加的drawable资源必须已经设置过初始位置、宽和高等信息。