在选择器中使用属性-Android [英] Use attributes in selector - Android

查看:88
本文介绍了在选择器中使用属性-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图允许用户设置自己的颜色主题. 我已经设法做到了

I'm trying to allow the user to set his own color themes. I've managed to accomplish this with

attr.xml

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

styles.xml

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

colors.xml

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

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

This works well for most of the application however I have a selector which previously had

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

现在它崩溃了.

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

Here's the logcat, any ideas on how to solve this?

 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)

编辑

在这里应用选择器

<?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支持的属性项目属性,在更高版本上,可以使用

Attrubutes supported from API 23 Item attributes, on later versions ColorStateList can be created programatically with AppCompatResources and used with required view.

选择器menu_item_text_color.xml

selector 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));

这篇关于在选择器中使用属性-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆