在selector-Android中使用属性

在selector-Android中使用属性,android,colors,attributes,themes,Android,Colors,Attributes,Themes,我试图允许用户设置自己的颜色主题。 我已经成功地完成了这项工作 attr.xml <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="extra_light" format="reference" /> </resources> <style name="Green" parent="@style/AppTheme"> <item name

我试图允许用户设置自己的颜色主题。 我已经成功地完成了这项工作

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="extra_light" format="reference" />
</resources>
<style name="Green" parent="@style/AppTheme">
    <item name="extra_light">@color/extra_light_green</item>
</style>
<resources>
    <color name="extra_light_green">#C5E26D</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/extra_light"/>
</shape>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="extra_light" format="reference" />
</resources>
<style name="Green" parent="@style/AppTheme">
    <item name="extra_light">@color/extra_light_green</item>
</style>
<resources>
    <color name="extra_light_green">#C5E26D</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/extra_light"/>
</shape>

@颜色/额外的浅绿色
colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="extra_light" format="reference" />
</resources>
<style name="Green" parent="@style/AppTheme">
    <item name="extra_light">@color/extra_light_green</item>
</style>
<resources>
    <color name="extra_light_green">#C5E26D</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/extra_light"/>
</shape>

#C5E26D
这适用于大多数应用程序,但我有一个以前使用过的选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/extra_light_green" />
</selector>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="?attr/extra_light" />
</selector>

现在它崩溃了

这是日志,有什么办法解决这个问题吗

 FATAL EXCEPTION: main
 android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
 at android.view.LayoutInflater.createView(LayoutInflater.java:683)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at com.android.internal.policy.impl.MiuiPhoneLayoutInflater.onCreateView(MiuiPhoneLayoutInflater.java:44)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:730)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:755)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:816)
at android.view.LayoutInflater.inflate(LayoutInflater.java:559)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:419)
致命异常:main
android.view.InflateException:二进制XML文件行#8:膨胀类时出错
位于android.view.LayoutInflater.createView(LayoutInflater.java:683)
com.android.internal.policy.impl.PhoneLayoutFlater.onCreateView(PhoneLayoutFlater.java:56)
位于com.android.internal.policy.impl.MiuiPhoneLayoutInflater.onCreateView(MiuiPhoneLayoutInflater.java:44)
位于android.view.LayoutInflater.onCreateView(LayoutInflater.java:730)
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:755)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:816)
在android.view.LayoutInflater.充气(LayoutInflater.java:559)
在android.view.LayoutInflater.充气(LayoutInflater.java:466)
在android.view.LayoutInflater.inflate(LayoutInflater.java:419)
编辑

这里是我应用选择器的地方

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/row_menu_title"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/selector_item_news"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="Medium Text"
        android:textSize="16sp" />

</LinearLayout>

我找到了解决此问题的方法。android似乎无法在预期可绘制的情况下应用颜色

创建名为item\u news\u drawable.xml的实体形状可绘制文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="extra_light" format="reference" />
</resources>
<style name="Green" parent="@style/AppTheme">
    <item name="extra_light">@color/extra_light_green</item>
</style>
<resources>
    <color name="extra_light_green">#C5E26D</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/extra_light"/>
</shape>

然后修改“selector\u item\u news.xml”


我使用颜色作为背景,而不是按下状态。但应用此代码后,textview的背景会因应用程序主题而发生变化


UPD:我已经在Android L preview上测试了我的解决方案。但在安卓4.4.2上,它也会崩溃,资源$NotFoundException。API 23支持的属性中的详细信息,在更高版本上,ColorStateList可以通过编程创建,并与所需视图一起使用

选择器菜单\u项\u文本\u color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/menu_item_text_color_selected" android:state_checked="true" />
    <item android:color="?attr/menu_item_text_color" /> <!-- Default -->
</selector>

在哪里应用选择器?选择器应用于TextView,然后发布TextView的代码。android:drawable=“@attr/extra_light”?At而不是问号,问号是一个HTML工件。它还与“@”一起崩溃,您应该何时使用“@”以及何时使用“?”?