问题描述
我和标签的应用程序有两个主题。在每个主题中的标签在选中和未选中状态不同的图像。我怎么能正确按主题参考图像?
My app with tabs has two themes. In each theme tabs have different images in selected and unselected state. How I can properly reference to image by theme?
例如。我在的themes.xml
For example. I have in 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
Also I have a tab_shows.xml and 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" />
我怎么能根据当前主题的选择参考所需的图像?这不是我的工作。
How I can reference to needed image in selector according to current theme?This not work for me
<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
In layout files this works, I mean reference to style via ?styleName
推荐答案
建立你的阶梯A和B型
在你的情况,你把机器人:可绘制=@可绘制/ ic_tab_shows_selected_light,而不是背景(我只是复制snipets从我的code) #000
in your case you put android:drawable="@drawable/ic_tab_shows_selected_light" instead of background (I just copied snipets from my code) #000
<style name="styleB">
<item name="android:background">#000</item>
</style>
你的主题A
your theme A
<style name="Theme.Blue">
<item name="pageBackground">@style/styleA</item>
</style>
主题B
<style name="Theme.Blue">
<item name="pageBackground">@style/styleB</item>
</style>
在你的attr.xml
in your attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="pageBackground" format="reference" />
</resources>
终于在你的widget你的风格=?pageBackground
finally in your widget you do style="?pageBackground"
这篇关于如何从绘制参考样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!