Sergey Sergheyev Sergey Sergheyev - 3 months ago 12
Android Question

How to set several backgrounds in one theme

I'm completely stuck(

UPD. Can it be possible to set different backgrounds (or other attributes) for separate Views by applying a single theme?

How should be applyed different styles for different views in different themes?
Briefly, I'd like to change themes, depend on the time of the day, to change view's attributes (color, background etc) accordingly.

Profi, any hint would be greatly appretiated.


Finally I've found a solution which is after my own heart! I suspected that these hardcoded methods are not very flexible solutions and need multiplexing code depends on the quantity of themes implemented. For thouse who faced the same quest pls have a look at the link

Declare attribute to use as style reference:

<declare-styleable name="CustomThemeViews">
<attr name="myViewStyle" format="reference"/>
</declare-styleable>     Define styles for each theme:

<style name="myViewStyleTheme01">
<item name="android:background">#f00</item>

<style name="myViewStyleTheme02">
<item name="android:background">#0f0</item>
</style>     Define themes that refer to different styles:

<style name="AppTheme" parent="android:Theme.Light">
<item name="myViewStyle">@style/myViewStyleTheme01</item>

<style name="AppThemeAlternative" parent="android:Theme.Light">
<item name="myViewStyle">@style/myViewStyleTheme02</item>
</style>     Use themed styles in layout

<ListView style="?attr/myViewStyle"
...     Now the style applied to the ListView changes according to the theme applied.