2016-12-09 30 views
1

當使用上的ImageView下面的XML屬性無效INT: 「RES /顏色/ abc_primary_text_material_light.xml」

android:tint="?android:attr/textColorPrimary" 

應用程序崩潰在某些設備上與該堆棧跟蹤:

Caused by: java.lang.NumberFormatException: Invalid int: "res/color/abc_primary_text_material_light.xml" 
    at java.lang.Integer.invalidInt(Integer.java:138) 
    at java.lang.Integer.parse(Integer.java:375) 
    at java.lang.Integer.parseInt(Integer.java:366) 
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123) 
    at android.content.res.TypedArray.getInt(TypedArray.java:254) 
    at android.widget.ImageView.<init>(ImageView.java:146) 
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60) 
at android.support.v7.wid 

爲什麼是這樣嗎?有沒有解決方案/解決方法?

+2

_crashes:是有一個很大的區別API級別之間?你指的是非公開價值,一些Android版本可能沒有這個價值(通常是舊版本)。 –

+0

@MateiRadu有沒有其他方法可以獲得主文本的顏色? – fiddler

+0

@Blackbelt my'values/colors.xml'是空的,因爲我不覆蓋或設置任何自定義顏色到目前爲止 – fiddler

回答

0

您正在嘗試使用某些API級別無法使用的私有資源。

它的標準來定義res/values/colors.xml你的顏色,如下面的例子:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="textColorPrimary">#212121</color> 

    <!-- other colors --> 
</resources> 

之後就可以引用該顏色如下:在一些devices_

android:tint="@color/textColorPrimary" 
+0

如果我想獲取當前主題的textColorPrimary而不覆蓋它,該怎麼辦? – fiddler

+0

您可以使用不同的名稱來定義您的自定義顏色,並在您喜歡的地方使用它。這樣,沒有明確定義色調或前景的其他文本元素將繼續使用主題顏色。 –

+0

或者,如果你想要一些android標準定義的顏色,你可以檢查[this](https://material.google.com/style/color.html#color-text-background-colors)官方指南的部分,並使用它們定義你的顏色。 –

相關問題