开发者

How to reference from drawable to style

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?

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</ite开发者_如何学JAVAm>
   </style>

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

In layout files this works, I mean reference to style via ?styleName


Build your style A and style B

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>

your theme A

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

theme B

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

in your attr.xml

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

finally in your widget you do style="?pageBackground"


You can find your answer here http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

Edit
(Additional information by Lukap in comments)

  1. Define one or more themes in themes.xml and set the definitions of your styles there.
  2. Define custom attributes, a.k.a. custom styles, in attrs.xml.
  3. Describe what the values of your custom styles are in styles.xml.

But you will need read more about the attrs.xml

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

Instead, we are referring to the value of some other attribute – activatedBackgroundIndicator – from our inherited theme. Whatever the theme defines as being the activatedBackgroundIndicator is what our background should be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜