最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android中xml中引用自己已定义的Color时出错:error: Error: No resource found that matches the given name (at ‘drawable’ with value ‘@android:color/ TabUnselectedStripColor’)

Android crifan 6878浏览 0评论

【问题】

Android中,之前已经在:

/res/values/themeConfig.xml

中定义好了一个Color:

<color name="TabUnselectedStripColor">#B1ADAD</color>

然后在另外的:

/res/drawable/tab_indicator_ab_mmstyle.xml

中想要引用这个Color:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <!-- <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" /> -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/TabUnselectedStripColor" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_mmstyle" />
    .....

</selector>

但是出错了:

error: Error: No resource found that matches the given name (at ‘drawable’ with value ‘@android:color/  TabUnselectedStripColor’)

如图:

Error No resource found that matches the given name  color

【解决过程】

1.很明显,就是不知道如何引用color。

搜:

android reference style color

参考:

android – Using Color Reference in Styles – Stack Overflow

去试试:

<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@color/TabUnselectedStripColor" />

结果真的就可以了。。。

2.然后才注意到,我之前就已经实现过类似的color引用了:

【已解决】Android程序中通过styles.xml自定义AlertDialog的背景色

之前已经用了这种写法:

    <color name="VariableLabel">#2A2598</color>

    <style name="Variable.Label">
        <item name="android:textColor">@color/VariableLabel</item>
    </style>

的。

 

【总结】

Android中xml中想要引用已经自己定义好的color等style的话,则直接去用:

@color/xxx

即可。

对应着android系统本身定义的好的,则是:

@android:color/xxx

 

举例:

别处定义:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    ......
    <color name="TabSelectedStripColor">#2A2598</color>
    <color name="TabUnselectedStripColor">#B1ADAD</color>
    ......
</resources>

引用的写法是:

<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@color/TabUnselectedStripColor" />

即可。

对应的应用android中的已定义好的color的写法是:

<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />

转载请注明:在路上 » 【已解决】Android中xml中引用自己已定义的Color时出错:error: Error: No resource found that matches the given name (at ‘drawable’ with value ‘@android:color/ TabUnselectedStripColor’)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.164 seconds, using 22.16MB memory