下面的ImageView Tint l无法参考主题属性



我最近在我的应用中添加了不同的主题,因此需要对每个主题的图标都不同。

<ImageView
        ...
        android:src="@drawable/ic_info"
        android:tint="?colorControlNormal"

但是,在低于21的API级别上这不起作用。我正在使用vectordrawables,并且已经尝试使用

vectorDrawables.useSupportLibrary = true

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

但这也没有帮助。

着色支持文档非常模棱两可,并且结合了使用vectordrawables和引用主题颜色,我找不到任何信息。

当前尝试使用:

<android.support.v7.widget.AppCompatImageView
    ...
    android:tint="?colorControlNormal"
    android:src="@drawable/icon"/>

导致:

Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class android.support.v7.widget.AppCompatImageView
   at android.view.LayoutInflater.createView(LayoutInflater.java:621)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
...
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.constructNative(Native Method)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
...
Caused by: java.lang.NumberFormatException: Invalid int: "res/color/abc_secondary_text_material_light.xml"
   at java.lang.Integer.invalidInt(Integer.java:137)
   at java.lang.Integer.parse(Integer.java:374)
   at java.lang.Integer.parseInt(Integer.java:365)
   at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:122)
   at android.content.res.TypedArray.getInt(TypedArray.java:255)
   at android.widget.ImageView.<init>(ImageView.java:155)
   at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
   at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)

终于找到了问题!

通过引用主题颜色?colorControlNormal

您隐式使用colorStateList(abc_secondary_text_material_light.xml),在l下不支持。除非您使用<android.support.v7.widget.AppCompatImageView>,并且还使用支持LIB app:tint属性的Tint属性。因此,正确的色彩属性在最后解决了。

尝试使用:

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/my_appcompat_imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_image" // change to ur own.
    android:tint="#636363" // also change this part to our own case
/>

这项工作给API&lt;19

最新更新