【问题标题】:Is it possible to change material design icon color from xml in Android?是否可以在 Android 中从 xml 更改材料设计图标颜色?
【发布时间】:2015-06-29 17:20:42
【问题描述】:

我实际上是在尝试在我的应用程序中使用彩色图标。我已经从here 下载了官方的材料设计图标包。现在这个包中的所有图标都是白色、灰色或黑色。但我希望图标具有不同的颜色。 类似于this image 左侧的图标。电话电话和邮件图标为蓝色。我怎样才能做到这一点?

【问题讨论】:

  • 你尝试过类似 android:background="@color/red" 吗?
  • 由于图标文件是 .png 格式,您应该使用创建图标的工具更改颜色。并使用 ImageAsset 将其添加到项目中。
  • @rajatIIT android:background 在图标周围制作了一个红色框,图标仍然保持灰色。
  • 您的最低目标 API 是多少?
  • 在 github 我们有图标的 svg 文件,我们可以从那里派生彩色图标吗?

标签: android icons android-5.0-lollipop material-design


【解决方案1】:

更改图标颜色试试

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

    android:tint="YourColor"
    />

注意:色调颜色是在图像上绘制的,而不是替代颜色。因此,在黑色图像上着色#80ff0000 会给您黑色上 50 % 的红色,而不是背景上的 50 % 红色。 IE。这不等同于 iOS 模板图像。

【讨论】:

  • 欢迎来到 Stack Overflow!请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为什么会解决问题的解释。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。如果只是猜测,还建议您不要发布答案。一个好的答案将有一个合理的理由说明它为什么可以解决 OP 的问题。
【解决方案2】:

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


Xml

<TintImageView
android:layout_width=""
android:layout_height="" 
android:src=""
android:backgroundTint="@color/green"/>

<ImageView 
android:tint="the_color_you_want"/>

以编程方式

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


所以上面的 xml 会将 imageView 着色为绿色,这意味着它会将 imageview 的每个像素着色为绿色。

【讨论】:

  • 如果我想更改 EditText TextView 的可绘制左|右图标 Tint,我觉得没有办法! android:backgroundTint="@android:color/holo_blue_bright" 不起作用!!
【解决方案3】:

我一直在寻找相同的东西,但要在代码中动态更改它。我希望这对寻找相同内容的人有所帮助。

为图标创建一个 ImageView 并在该视图上使用 setColorFilter。

ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
imageViewIcon.setColorFilter(getContext().getResources().getColor(R.color.blue));

【讨论】:

    【解决方案4】:
    <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>
    

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

    【讨论】:

      【解决方案5】:

      如何在 XML 中更改材质图标颜色

      <ImageButton 
           android:layout_width="YourValue"
           android:layout_height="YourValue"
           ...
           android:tint="YourColor" // this line do the magic
      />
      

      【讨论】:

        【解决方案6】:

        此线程中已经有许多替代方案。也许我可以再添加一个给觉得方便的人:

        也可以使用Drawable类,代码如下

            Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.ic_play_circle_filled);
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(drawable, getResources().getColor(R.color.colorPrimary));
        

        虽然上述方法对我有用,但对于我的用例来说,使用android:tint

        【讨论】:

          【解决方案7】:

          是的,这是可能的,而且使用这个库很简单: https://github.com/jrvansuita/IconHandler

          你可以很容易地这样称呼:

          Icon.on(yourImageView).color(R.color.your_color).icon(R.mipmap.your_icon).put();
          

          【讨论】:

            【解决方案8】:

            使用以下代码从 xml 修改图标的颜色

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ...
                android:foregroundTintMode="src_over"
                android:tint="Your colour"
                ...
                app:srcCompat="@android:drawable/ic_menu_slideshow" 
            />
            

            【讨论】:

              【解决方案9】:

              除了 Murali 所说的,制作多个具有不同填充颜色的可绘制对象。

              然后在需要时添加到您的代码中:

              imageView.setImageResource(R.drawable.drawable_with_different_color)
              

              不是:imageView.setBackgroundResource()

              【讨论】:

              • 如果假设图标必须以 200 种不同颜色显示,那么您会在 drawable 中放置 200 个不同颜色的图标吗?
              猜你喜欢
              • 2015-08-13
              • 2017-05-19
              • 2015-08-15
              • 2014-10-12
              • 1970-01-01
              • 2023-03-21
              • 1970-01-01
              • 2019-08-12
              • 2015-02-06
              相关资源
              最近更新 更多