How can I set the drawableRight resource via code?

ぐ巨炮叔叔 提交于 2019-12-29 06:44:07

问题


I am trying to set an image on the right side of my button after the button has been clicked. I want to do this via code.

I have seen how to change the background resource via code but I am not able to find any examples showing how to change the sides via code. Is it possible?


回答1:


You need to use the

public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, 
 Drawable bottom)

method with null for any that are not needed.




回答2:


Usually you can change using this

Drawable draw = getResources().getDrawable(R.drawable.facebook);
myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);

Be aware you can miss the button text.



来源:https://stackoverflow.com/questions/7380587/how-can-i-set-the-drawableright-resource-via-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!