我在attrs.xml了Android的顏色選擇不使用自定義屬性的工作
<resources>
<!-- theme specific colors -->
<attr format="reference|color" name="foreground" />
<attr format="reference|color" name="background" />
</resources>
然後在theme.xml
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="foreground">#0000FF</item>
<item name="background">#00FF00</item>
</style>
我還創建顏色選擇器名爲forground_to_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="?background"/> <!-- pressed -->
<item android:state_focused="true" android:color="?background"/> <!-- focused -->
<item android:color="?foreground"/> <!-- default -->
</selector>
現在我想在TextView中一起使用它:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/forground_to_background" />
遺憾的是它不工作。我只有一種顏色 - 紅色,而不是有很好的綠色藍色。 TextView始終是紅色的。當我改變TextView使用「前景」顏色會改變。此外,當我更改顏色從「?xxxx」選擇硬編碼值爲「#00f」顏色開始工作。
問題在哪裏?我究竟做錯了什麼?
編輯: 我認爲這是重複的問題/錯誤Can a selector resource use a color defined in a style?
EDIT2的: 此外,當我嘗試使用這個TextView的ListView中的應用程序崩潰。它無法解析XML。
顯然,這個問題被固定在Android L移動預覽,詳情點擊這裏:https://code.google.com/p/android/issues/detail?id=26251 –
你找到你的問題的答案嗎?我有同樣的問題 – Pavya