2011-09-23 82 views
8

我的带有选项卡的应用程序有两个主题。在每个主题标签中,选择和未选择状态下都有不同的图像。我如何正确引用图像的主题?如何从可绘制风格引用

例如。我在的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来

+0

得到了同样的问题在这里:http://stackoverflow.com/q/12115125/317889 – HGPB

+0

我在做类似的事情HERE !!! http://stackoverflow.com/questions/17103894/overriding-referenced-style-attributes – toobsco42

回答

2

您可以在这里找到你的答案 http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

编辑
(Lukap在评论中的补充信息)

  1. themes.xml中定义一个或多个主题,并在其中设置样式的定义。
  2. attrs.xml中定义自定义属性,又名自定义样式。
  3. 描述自定义样式的值在styles.xml中。

但是,你需要阅读更多有关attrs.xml

<item name="android:background">? android:attr/activatedBackgroundIndicator</item> 
</style> 

取而代之的是,我们指的是一些其他的属性值 - activatedBackgroundIndicator - 从我们继承的主题。无论主题定义为activatedBackgroundIndicator是我们的背景应该是什么。

+0

这是非常好的教程 – Lukap

+1

谢谢。本教程使用了我的应用程序主题。然而它对我的问题没有答案。 –

+0

thre是第1行的答案。在themes.xml中定义一个或多个主题,并在其中设置样式的定义。 2.在attrs.xml中定义自定义属性,a.k.a.自定义样式。 3.描述您的自定义样式在styles.xml中的值。但是您需要阅读更多关于attrs.xml – Lukap

5

建立自己的风格A和风格B

你的情况

你把android:drawable="@drawable/ic_tab_shows_selected_light"而不是背景(我只是从我的代码复制snipets) #000

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

您的主题

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

终于在你的widget你做style="?pageBackground"

+0

注意在你的情况下,而不是你把android:drawable =“@ drawable/ic_tab_shows_selected_light”等等的背景,但是这样做的方法 – Lukap

+0

谢谢,我会尽力做到这一点。 –

+1

嗯。多样式这是好的,我明白了。但我不明白如何将这种方法应用于选择器可绘制文件。我创建选项卡并传递给它drawable resourceId(选择器文件)我如何在此选择器中设置正确的图像(按主题)。引用如?styleNameToOtherDrawable不起作用。也不适用于代码spec.setIndicator(res.getDrawable(R.attr.tabShows)); –