在选择器中使用属性 - Android

Mik*_*ant 5 android attributes themes colors

我试图让用户设置自己的颜色主题.我已经设法完成了这个

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="extra_light" format="reference" />
</resources>
Run Code Online (Sandbox Code Playgroud)

styles.xml

<style name="Green" parent="@style/AppTheme">
    <item name="extra_light">@color/extra_light_green</item>
</style>
Run Code Online (Sandbox Code Playgroud)

colors.xml

<resources>
    <color name="extra_light_green">#C5E26D</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/extra_light_green" />
</selector>
Run Code Online (Sandbox Code Playgroud)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="?attr/extra_light" />
</selector>
Run Code Online (Sandbox Code Playgroud)

现在它崩溃了.

这是logcat,关于如何解决这个问题的任何想法?

 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)
Run Code Online (Sandbox Code Playgroud)

编辑

这是我应用选择器的地方

<?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>
Run Code Online (Sandbox Code Playgroud)

Mik*_*iuk 1

我已经找到了解决此问题的方法。当需要绘制时,Android 似乎无法应用颜色。

创建一个名为item_news_drawable.xml的实体形状可绘制对象

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/extra_light"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

然后修改你的“selector_item_news.xml”

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/item_news_drawable" />
</selector>
Run Code Online (Sandbox Code Playgroud)

我使用背景颜色而不是按下状态。但是应用此代码后,文本视图的背景根据应用程序主题发生了变化。

UPD:我已经在 Android L 预览版上测试了我的解决方案。但在 Android 4.4.2 上,它也会崩溃并出现 Resources$NotFoundException。详细内容在这个答案里