【问题标题】:Can provided Android SDK icons be set with color?提供的 Android SDK 图标可以设置颜色吗?
【发布时间】:2016-10-16 22:57:15
【问题描述】:

Android SDK 提供以下图标。

有没有办法为那些..设置颜色,如果可能,如何设置?

    <ImageView
        android:id="@+id/share_button"
        style="@style/IconNav"
        android:src="@android:drawable/ic_menu_share"/>

    <ImageView
        android:id="@+id/bookmark_button"
        style="@style/IconNav"
        android:src="@android:drawable/ic_input_get"/>

更新

在完全刷新项目后,Xml 中的 tint 属性起到了作用。

简答题

.. 这是对我有用的解决方案 - 将属性添加到 ImageView xml:

    android:tint="@color/grass_dark"

@goldenb 的答案是对解决此问题的不同方法的全面介绍,因此我将其标记为答案。

【问题讨论】:

    标签: android android-icons


    【解决方案1】:

    您确实可以使用色调来更改ImageView 的颜色,但是您应该注意android:tint 将始终应用于原始颜色之上。

    如博主danlew所述

    • ImageView 的色调将色调颜色与原始资源混合。你想要的是让色调完全接管;取而代之的是 在现有颜色之上应用色调。因此,例如,如果 源资产是黑色的,您希望它是 #77FFFFFF(半透明 白色的阴影),你实际上最终会得到那个白色的阴影 下面是黑色背景。

    • android:tint 仅限于 ImageView。您希望能够在任何 View 中为任何 Drawable 着色。

    一种可能的替代方法是让您使用 android ColorFilter

    根据official documentation

    颜色过滤器可以与 Paint 一起使用,以修改使用该 Paint 绘制的每个像素的颜色。这是一个绝对不能直接使用的抽象类。

    你可以用 ColorFilter 做很多或多或少复杂的事情,但你如何应用它呢?

    一个简单的example 来自另一个所以问题是:

    //White tint
    imageView.setColorFilter(Color.argb(255, 255, 255, 255)); 
    
    or
    
    imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))
    

    或者来自here的SO中更完整的答案

    ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
    ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
    ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
    
    
    
    // we can create the color values in different ways:
    redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
    greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
    blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
    

    如果您想了解更多信息,请查看这些链接

    SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

    setColorFilter()

    Fast Android asset theming with ColorFilter

    SO-Modifying the color of an android drawable

    【讨论】:

    • @gnB 你能解决你的问题吗?我相信这个答案应该被标记为正确,如果这有帮助,请告诉我=)
    • 这个在 Java 中设置颜色的解决方案与使用 android:tint 一样有效。与其他方法一样,我必须清理/刷新项目才能使其正常工作。感谢您的指针和详细信息
    • @gnB 我的荣幸!
    【解决方案2】:

    您可以使用带有色调的位图。将此添加到您的 drawables 文件夹中。

    ic_input_get_colored.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:src="@android:drawable/ic_input_get"
            android:tint="@color/yourDesiredColor"/>
    

    【讨论】:

    • 这与 &lt;ImageView ../&gt; 标签一起使用。我必须清理/刷新项目才能使其正常工作。
    • 太棒了。祝你好运。
    猜你喜欢
    • 2016-09-14
    • 2016-10-06
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多