How to reference a style drawing

There are two topics in my tabbed application. The tabs for each topic have different images in the selected and unselected state. How can I refer to a related image correctly?

For example. I have in theme.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> 

I also have 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 can I refer to the required image in the selector according to the current theme? This does 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 the layout files, this works, I mean the style link through? styleName

+8
android reference image styles themes
Sep 23 '11 at 13:19
source share
2 answers

Create your own style A and style B

in your case you put android:drawable="@drawable/ic_tab_shows_selected_light" instead of the background (I just copied snipets from my code) # 000

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

your topic a

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

topic 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 that you do style="?pageBackground"

+5
Sep 23 '11 at 14:34
source share

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

Edit
(See Lukap for more info in the comments)

  • Define one or more themes in themes.xml and define your styles.
  • Define user attributes, custom aka styles, in attrs.xml .
  • Describe what the values ​​of your custom styles are in styles.xml .

But you need to know more about attrs.xml

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

Instead, we refer to the value of some other attribute - activatedBackgroundIndicator - from our inherited theme. No matter what defines the theme as activatedBackgroundIndicator , what our background should be.

+2
Sep 23 2018-11-11T00:
source share



All Articles