是否可以在Android中从xml更改材质设计图标颜色?

Aks*_*hay 29 icons android material-design android-5.0-lollipop

我实际上是想在我的应用程序中使用彩色图标.我从这里下载了官方的材料设计图标包.现在这个包中的所有图标都是白色,灰色或黑色.但我希望图标颜色不同.类似于此图像左侧的图标.电话和邮件图标是蓝色的.我怎么能做到这一点?

小智 53

如需更改图标颜色,请尝试

<ImageButton
    android:layout_width="your value"
    android:layout_height="your value"
    /* and so on ... */

    android:tint="YourColor"
    />
Run Code Online (Sandbox Code Playgroud)

注意:色调颜色涂在图像的顶部,而不是替换颜色.因此#80ff0000,黑色图像上的色调会使黑色背景为50%红色,而背景背景则不会为50%红色.即这不等于iOS模板图像.


Rod*_*uin 33

您可以TintImageView在appcompat支持库中使用,然后通过简单地调用图像视图将图像视图调色android:backgroundTint为一种颜色来对图像视图进行着色/着色 .


XML

<TintImageView
android:layout_width=""
android:layout_height="" 
android:src=""
android:backgroundTint="@color/green"/>
Run Code Online (Sandbox Code Playgroud)

要么

<ImageView 
android:tint="the_color_you_want"/>
Run Code Online (Sandbox Code Playgroud)

编程方式

ImageView yourImageView = findViewById(...) yourImageView.setColorFilter(Context.getColor(your_color_here))


因此,上面的xml会将imageView着色为绿色,这意味着它会将图像视图的每个像素着色为绿色可见.


小智 11

我一直在寻找相同的东西,但在代码中动态地改变它.我希望这有助于某人寻找同样的事情.

为图标创建ImageView并在该视图上使用setColorFilter.

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


Mur*_*ali 5

<vector android:height="48dp" android:viewportHeight="24.0"
    android:viewportWidth="24.0" android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#ca0f0f" android:pathData="M3,5v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2H5c-1.11,0 -2,0.9 -2,2zm12,4c0,1.66 -1.34,3 -3,3s-3,-1.34 -3,-3 1.34,-3 3,-3 3,1.34 3,3zm-9,8c0,-2 4,-3.1 6,-3.1s6,1.1 6,3.1v1H6v-1z"/>
</vector>
Run Code Online (Sandbox Code Playgroud)

你通过改变 android:fillColor="#ca0f0f" 来改变颜色