2011-09-23 79 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)); –