How to change color of CompoundDrawable on Button?

前端 未结 4 1071
渐次进展
渐次进展 2020-12-16 05:01

I am trying to figure out how to change the color of icon which is in drawable left of button.

Below is the XML code I am using :

  
4条回答
  •  一生所求
    2020-12-16 05:34

    You can set tint programmatically like below:

    int tintColor = ContextCompat.getColor(context, android.R.color.darker_gray);
    
    Button button = (Button) findViewById(R.id.button);
    
    Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.ic_email_black_18dp);
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable.mutate(), tintColor);
    
    drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    
    button.setCompoundDrawables(drawable, null, null, null);
    

    Or you can use library Support Drawable Tints by snodnipper.
    This library enables to set tint for drawableLeft of Button.
    https://github.com/snodnipper/android-appcompat-issue198613

提交回复
热议问题