Android Tint VectorDrawable内部可绘制资源xml

Android Tint VectorDrawable内部可绘制资源xml,android,android-resources,tint,android-vectordrawable,Android,Android Resources,Tint,Android Vectordrawable,我有一个可供选择的按钮/图像视图的可绘制资源,如下所示: <selector> <item android:state_selected="true"> <layer-list> <item> <shape android:shape="oval"> <solid android:color="@color/blue"/>

我有一个可供选择的按钮/图像视图的可绘制资源,如下所示:

<selector>
<item android:state_selected="true">
    <layer-list>
        <item>
            <shape android:shape="oval">
                <solid android:color="@color/blue"/>
            </shape>
        </item>
        <item>
            <bitmap
                android:src="@drawable/ic_icon"
                android:tint="@color/white"/>

        </item>
    </layer-list>
</item>
<item>
    <layer-list>
        <item>
            <shape android:shape="oval">
                <solid android:color="@color/background_unselected"/>
            </shape>
        </item>
        <item>
            <bitmap
                android:src="@drawable/ic_icon"
                android:tint="@color/icon_unselected"/>
        </item>
    </layer-list>
</item>

由于我已切换到使用VectorDrawable,上述声明不起作用,因为我无法引用带有
标记的VectorDrawable。 但据我所知,这是我唯一能给图标着色的方法

我也不能在代码中应用颜色过滤器,因为这会使整个可绘制的图像着色,而不仅仅是图标


有什么建议吗

您可以将颜色定义为属性,并在SVG中引用它们。然后可以加载一个主题的可绘制样式

简短示例:

attributes.xml

<declare-styleable name="vectorColors">
    <attr name="primaryColor" format="color" />
    <attr name="secondaryColor" format="color" />
</declare-styleable>
<path
    android:fillColor="?attr/primaryColor"
    android:pathData="...." />
<path
    android:fillColor="?attr/secondaryColor"
    android:pathData="...." />
<style name="svgColors">
    <item name="primaryColor">@color/someColor</item> 
    <!-- also can use #RRGGBB way -->
    <item name="secondaryColor">#ff0000</item>
</style>
在您的特定选择器中,您应该替换

<item>
    <bitmap
        android:src="@drawable/ic_icon"
        android:tint="@color/white"/>
</item>

通过


没有。他们没有。解决方案无效。它正在通过代码运行。我的案例是根据当前主题将矢量白色路径更改为另一种颜色。我做到了:1。声明的字段
2。将attr添加到我的主题
@color/colorforthistorheme
3。通过
drawable-drawable=AppCompatResources.getDrawable(context,drawableId),从代码中调用以获取drawable,知道它的资源id3。Glide.IMHO加载的图像(在我的例子中)的关键是使用可绘制的当前主题-我使用了
AppCompatResources
,它本身可以找到现在的主题。这不起作用,需要位图可绘制,这与XmlPullParserException崩溃“需要有效的'src'属性”
<item>
    <bitmap
        android:src="@drawable/ic_icon"
        android:tint="@color/white"/>
</item>