【问题标题】:android - refer to a color inside a shapeandroid - 指形状内的颜色
【发布时间】:2012-06-24 21:09:21
【问题描述】:

我想在一个形状中引用我自定义的颜色属性,它总是会导致错误。

首先我声明属性:

<resources>
    <attr name="divider_color" format="color|reference" />
</resources>

在我的主题中,我声明了值:

 <style name="myTheme" parent="android:style/Theme.Dialog">
          <item name="divider_color">@color/red</item>
 </style>

如果我这样访问它,它工作正常:

 android:background="?divider_color"/>

但我想在形状内使用它:

<shape
    <gradient
        android:angle="0"
        android:centerColor="?divider_color"
        android:endColor="#00000000"
        android:startColor="#00000000"
        android:type="linear" />

</shape>

我收到此错误:

E/AndroidRuntime(3117):原因:java.lang.UnsupportedOperationException:无法转换为颜色:type=0x2

知道如何解决这个问题吗?

【问题讨论】:

    标签: android themes


    【解决方案1】:

    这是 Android 中的一个错误。这已在 Lollipop 中修复,因此应该可以在 L&M 上使用,但在以前的设备上它会崩溃

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        我不知道。因此,我检查了该异常源自操作系统的位置:http://androidxref.com/4.0.4/xref/frameworks/base/core/java/android/content/res/TypedArray.java#326

        307    public int getColor(int index, int defValue) {
        308        index *= AssetManager.STYLE_NUM_ENTRIES;
        309        final int[] data = mData;
        310        final int type = data[index+AssetManager.STYLE_TYPE];
        311        if (type == TypedValue.TYPE_NULL) {
        312            return defValue;
        313        } else if (type >= TypedValue.TYPE_FIRST_INT
        314            && type <= TypedValue.TYPE_LAST_INT) {
        315            return data[index+AssetManager.STYLE_DATA];
        316        } else if (type == TypedValue.TYPE_STRING) {
        317            final TypedValue value = mValue;
        318            if (getValueAt(index, value)) {
        319                ColorStateList csl = mResources.loadColorStateList(
        320                        value, value.resourceId);
        321                return csl.getDefaultColor();
        322            }
        323            return defValue;
        324        }
        325
        326        throw new UnsupportedOperationException("Can't convert to color: type=0x"
        327                + Integer.toHexString(type));
        328    }
        

        看起来“类型”的解释不正确。此方法认为您正在传递类型为TYPE_ATTRIBUTE 的属性,如here 所示。这并不能回答问题,但也许它可以帮助您缩小范围。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-25
          • 2017-09-03
          相关资源
          最近更新 更多