android – 如何从drawable引用到样式

带有标签的我的应用有两个主题.在每个主题中,选项卡具有处​​于选定和未选定状态的不同图像.我如何通过主题正确引用图像?

例如.我有themes.xml

<?xml version="1.0" encoding="utf-8"?>

<style name="LightTheme" parent="@android:style/Theme.Light">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_light</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_light</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_light</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_light</item>
</style>

<style name="DarkTheme" parent="@android:style/Theme.Black">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_dark</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_dark</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_dark</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_dark</item>
   </style>

我还有一个tab_shows.xml和tab_news.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="@drawable/ic_tab_shows_selected_light"/>
<item  android:state_selected="false" android:drawable="@drawable/ic_tab_shows_unselected_light" />

如何根据当前主题在选择器中引用所需的图像?
这不适合我

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="?tabShowsSelected"/>
<item  android:state_selected="false" android:drawable="?tabShows" />

在布局文件中这是有效的,我的意思是通过?styleName引用样式

解决方法

建立你的风格A和风格B.

在你的情况下你把android:drawable =“@ drawable / ic_tab_shows_selected_light”而不是背景(我只是从我的代码中复制了snipets)

#000

<style name="styleB">
        <item name="android:background">#000</item>
    </style>

你的主题A.

<style name="Theme.A">
        <item name="pageBackground">@style/styleA</item>
    </style>

主题B.

<style name="Theme.Blue">
        <item name="pageBackground">@style/styleB</item>
    </style>

在你的attr.xml中

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

最后在你的小部件中你做了style =“?pageBackground”

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...