【问题标题】:Use attributes in selector - Android在选择器中使用属性 - Android
【发布时间】:2013-10-11 12:56:54
【问题描述】:

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

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>

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

<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,有什么解决方法的想法吗?

 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>

【问题讨论】:

  • 你在哪里应用选择器?
  • 选择器应用于文本视图
  • 然后贴出TextView的代码
  • android:drawable="@attr/extra_light"?在而不是问号,这是一个 HTML 工件。
  • 它也与“@”一起崩溃,什么时候应该使用“@”,什么时候应该使用“?” ?

标签: android colors attributes themes


【解决方案1】:

API 23 Item attributes 支持的属性,在更高版本上,可以使用 AppCompatResources 以编程方式创建 ColorStateList 并与所需的视图一起使用。

选择器 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));

【讨论】:

    【解决方案2】:

    我找到了解决此问题的方法。预计可绘制对象时,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>
    

    然后修改你的“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>
    

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

    UPD:我已经在 Android L 预览版上测试了我的解决方案。但在 Android 4.4.2 上,它也会因 Resources$NotFoundException 而崩溃。详情在this answer

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 2015-11-23
      • 2020-07-20
      • 2020-07-05
      相关资源
      最近更新 更多