如何在Android中为imageview设置颜色

Moh*_*uri 47 android

我想设置图标ImageView,我从这个网站下载图标:FlatIcon
现在我想要设置这个图标的颜色,但使用时setBackground只需添加背景颜色,而不是设置为图标!
使用时,NavigationView我可以使用以下代码将颜色设置为图标:app:itemIconTint="@color/colorAccent".

如何将颜色设置为图标ImageViewitemIconTint?谢谢所有<3

El0*_*din 123

如果您使用的是图标,这可能很有用:

android:tint="@color/colorAccent"
Run Code Online (Sandbox Code Playgroud)

否则你可以尝试修改类:

ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
imageViewIcon.setColorFilter(getContext().getResources().getColor(R.color.blue));
Run Code Online (Sandbox Code Playgroud)

此主题中的更多信息:是否可以在Android中更改xml中的材质设计图标颜色?

  • 如果你看到getColor已被弃用,你需要执行以下操作:```imageViewIcon.setColorFilter(ContextCompat.getColor(getContext(),R.color.blue));``` (10认同)

sJy*_*sJy 23

使用ImageView的tint属性.

 android:tint="@color/colorAccent"
Run Code Online (Sandbox Code Playgroud)


Sha*_*aur 8

使用

imageView.setColorFilter(getResources().getColor(R.color.color_white)); 
Run Code Online (Sandbox Code Playgroud)


小智 7

 DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(getApplicationContext(), R.color.white));
Run Code Online (Sandbox Code Playgroud)


Ign*_*spo 5

最新的 Lint 使用 android:tint 显示警告,建议使用 app:tint,但在与 app:tintMode 一起使用之前,tint 是不可见的。所以它看起来像这样:

app:tint="@color/yourcolor"
app:tintMode="add"
Run Code Online (Sandbox Code Playgroud)