How to add button tint programmatically

前端 未结 15 939
时光取名叫无心
时光取名叫无心 2020-11-30 23:38

In the new AppCompat library, we can tint the button this way:

15条回答
  •  情歌与酒
    2020-12-01 00:41

    You could use

    button.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.blue_100)));
    

    But I would recommend you to use a support library drawable tinting which just got released yesterday:

    Drawable drawable = ...;
    
    // Wrap the drawable so that future tinting calls work
    // on pre-v21 devices. Always use the returned drawable.
    drawable = DrawableCompat.wrap(drawable);
    
    // We can now set a tint
    DrawableCompat.setTint(drawable, Color.RED);
    // ...or a tint list
    DrawableCompat.setTintList(drawable, myColorStateList);
    // ...and a different tint mode
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);
    

    You can find more in this blog post (see section "Drawable tinting")

提交回复
热议问题