5

我正在尝试创建一个使用由所选主题定义的属性的选择器。

attrs.xml

<declare-styleable name="ThemeColors">
    <attr name="buttonTextColor" format="color" />
</declare-styleable>

选择器.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="@android:color/black" />
    <item android:color="?attr/buttonTextColor"/>
</selector>

主题:

<style name="Theme.Test" parent="@style/Theme.AppCompat">
    <item name="buttonTextColor">@android:color/white</item>
</style>

部分布局文件:

  <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test"
        android:textColor="@drawable/selector" />

显现

<application
    ...
    android:theme="@style/Theme.Test" >

布局文件被setContentView方法膨胀。

现在的问题是 textcolorRED处于未按下状态(按下时为黑色)。似乎找不到buttonTextColor并使用默认值RED(?)

已经尝试将主题设置为应用程序上下文,但没有运气..

提前致谢。

4

0 回答 0