How to change searchview icon color?

后端 未结 9 1264
北海茫月
北海茫月 2020-12-19 13:19

I know there\'s already a lot of answers for this question here, and I wouldn\'t be creating another thread if I hadn\'t already tried everything I saw here. Anyway, the qu

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 14:06

    Take the Drawable you want to tint, wrap it with a tinted drawable , and set it as a new one for the search menu item:

    @JvmStatic
    fun getTintedDrawable(inputDrawable: Drawable, @ColorInt color: Int): Drawable {
        val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
        DrawableCompat.setTint(wrapDrawable, color)
        DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
        return wrapDrawable
    }
    

    Usage:

    val drawable =getTintedDrawable(...)
    searchMenuItem.icon = drawable
    

    And this should work for all Android versions that android-x (support library) supports.

提交回复
热议问题