【问题标题】:ImageView tint below L cannot reference Theme AttributeL 下面的 ImageView tint 不能引用 Theme 属性
【发布时间】:2017-09-23 09:50:46
【问题描述】:

我最近为我的应用添加了不同的主题,因此需要为每个主题设置不同的图标。

<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)

【问题讨论】:

    标签: android android-layout android-theme android-vectordrawable


    【解决方案1】:

    终于找到问题了!

    通过引用主题颜色?colorControlNormal

    您隐式使用 L 以下不支持的 ColorStateList (abc_secondary_text_material_light.xml)。 除非您使用 &lt;android.support.v7.widget.AppCompatImageView&gt; 并且还使用支持库 app:tint 属性中的 tint 属性。 所以适当的 tint 属性最终解决了它。

    【讨论】:

      【解决方案2】:

      尝试使用:

      <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

      【讨论】:

      • 我认为 ImageView 会自动替换为 AppCompatImageView。问题是,我需要为色调引用主题属性/颜色,并且在使用您提供的解决方案时仍然会导致崩溃
      • 在原始问题中添加了示例
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 2018-08-17
      相关资源
      最近更新 更多