在选择器中使用属性 - Android

2023-12-23

我试图允许用户设置自己的颜色主题。 我已经成功地做到了这一点

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="extra_light" format="reference" />
</resources>

样式.xml

<style name="Green" parent="@style/AppTheme">
    <item name="extra_light">@color/extra_light_green</item>
</style>

颜色.xml

<resources>
    <color name="extra_light_green">#C5E26D</color>
</resources>

这对于大多数应用程序都很有效,但是我有一个以前有过的选择器

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

to

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

现在它崩溃了。

这是 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)

EDIT

这是我应用选择器的地方

<?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>

API 23 支持的属性物品属性 https://developer.android.com/reference/android/content/res/ColorStateList.html,在更高版本上 ColorStateList 可以通过编程方式创建应用程序兼容性资源 https://developer.android.com/reference/android/support/v7/content/res/AppCompatResources.html并与所需视图一起使用。

选择器menu_item_text_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>

在源代码中

navigationView.setItemTextColor(AppCompatResources.getColorStateList(this, R.color.menu_item_text_color));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在选择器中使用属性 - Android 的相关文章

随机推荐