How to reference from drawable to style

限于喜欢 提交于 2019-12-18 15:54:23

问题


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


回答1:


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"




回答2:


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.



来源:https://stackoverflow.com/questions/7529574/how-to-reference-from-drawable-to-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!