是否可以通过java更改按钮上的drawable?
例子; 我有一个这样的按钮。
<Button
style="@style/Buttons"
android:id="@+id/butFavTeam"
android:drawableBottom="@drawable/venue"/>
我想用我的可绘制目录中的另一个更改当前的 drawableBottom 图像。
你有没有看过这个方法:
/*
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
*/
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)
试试这个:
/*remember to first clear the callback of the drawable you are replacing to prevent memory leaks...
* Get the Drawables for the button by calling: myButton.getCompoundDrawables(), then loop through that calling myOldDrawable.setCallback(null);
* e.g:*/
for(Drawable myOldDrawable : myButton.getCompoundDrawables())
{
myOldDrawable.setCallback(null);
}
//use null where you don't want a drawable
myButton.setCompoundDrawablesWithIntrinsicBounds(null,null,null, myNewDrawable);
我希望这会有所帮助。
刚找到。
Drawable myIcon = this.getResources().getDrawable(R.drawable.myVenue);
butFavTeam.setCompoundDrawablesWithIntrinsicBounds(null, null, null, myIcon);
顺便说一句...确保您不会意外使用方法 setCompoundDrawables()。它采用与 setCompoundDrawablesWithIntrinsicBounds() 相同的参数,但显然也需要一些边界细节。
我因此而沦为牺牲品,因为我懒惰地接受了第一个预先输入的方法。我发布此内容以防您也无法弄清楚图像未显示的原因。