用java改变drawableBottom

use*_*183 2 java android drawable

是否可以通过java更改按钮上的drawable?

例; 我有一个像这样的按钮.

<Button 
style="@style/Buttons" 
android:id="@+id/butFavTeam"
android:drawableBottom="@drawable/venue"/>
Run Code Online (Sandbox Code Playgroud)

我想用我的drawable目录中的另一个更改当前的drawableBottom图像.

Rya*_*anM 7

你看过这个方法了吗:

/*
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)
Run Code Online (Sandbox Code Playgroud)

试试这个:

/*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);
Run Code Online (Sandbox Code Playgroud)

我希望有所帮助.

  • null是正确的.在他的例子中,他传递的是实际的Drawables,因此null是该方法版本的正确"无值".还有另一个版本,可能是你想到的,你传递四个可绘制的id值(即.int),而0是正确的东西,你不想要一个Drawable. (5认同)
  • NULL值为**不正确**.如果你不想在那里使用Drawable,请使用0 .setCompoundDrawablesWithIntrinsicBounds(0,0,0,myIcon); [来源](http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds%28int,%20int,%20int,%20int%29) (3认同)