我进入了attrs.xml
<resources>
<!-- theme specific colors -->
<attr format="reference|color" name="foreground" />
<attr format="reference|color" name="background" />
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在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>
Run Code Online (Sandbox Code Playgroud)
我还创建了名为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>
Run Code Online (Sandbox Code Playgroud)
现在我想在TextView中一起使用它:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/forground_to_background" />
Run Code Online (Sandbox Code Playgroud)
不幸的是它不起作用.我没有漂亮的绿蓝色,只有一种颜色 - 红色.TextView始终为红色.当我将TextView更改为使用"?foreground"时,颜色会发生变化.此外,当我将颜色选择器从"?xxxx"更改为硬编码值时,"#00f"颜色开始起作用.
哪里有问题?我究竟做错了什么?
编辑: 我认为它是问题/错误的重复选择器资源可以使用样式中定义的颜色吗?
Edit2: 此外,当我尝试在ListView应用程序中使用此TextView时崩溃.它无法解析XML.
android ×1