Change themes and styles based on the Android version of the assembly

I know that I can add different XML files for different API levels, for example, having different styles for values-v21 and values-v19. I'm trying to understand how the build system works with these different values? So, for example, if I have most of my styles common to all APIs, and one element of one style changes between 21 and the rest, do the following:

1) Copy all styles.xml in v21 and change the one value I need to change

2) Add only one style that has been changed to styles.xml under v21

3) Add only one element of this style that has changed under 21

This is confusing, and I could not find documentation on how the inline process handles merging styles.

+4
source share
2 answers

The rules are pretty clear:

  • When you start Android , the style with the best match is selected
  • If the selected style is a child, Android combines its elements with the parent best-match style.

    If you provide your mutable element via a link, simply define its value to match the selected api version.

    <style name="SomeStyle">
        <item name="someColor">@color/some_color</item>
    </style>
    

You can have some_color.xmlin the folder color-v21for API 21 and a common version of this file in the folder colorfor all other api levels.

Example:

Do you want to have the following style for non-v21 API

    <style name="FinalStyle">
        <item name="commonText">It\ a common text</item>
        <item name="specificDrawable">@drawable/icon</item>
        <item name="specificColor">@color/primary_color</item>
        <item name="specificText">non-v21</item>
    </style>

And the following style for API v21

    <style name="FinalStyle">
        <item name="commonText">It\ a common text</item>
        <item name="specificDrawable">@drawable/icon</item>
        <item name="specificColor">@color/secondary_color</item>
        <item name="specificText">v21</item>
    </style>

Specific parameters differ between API v21 / non-v21, common parameters are common.

How to do it?

  • res/values/styles.xml

    <style name="BaseStyle">
        <item name="commonText">It\ a common text</item>
        <item name="specificDrawable">@drawable/icon</item>
    </style>
    
    <style name="FinalStyle" parent="BaseStyle">
        <item name="specificColor">@color/primary_color</item>
        <item name="specificText">non-v21</item>
    </style>
    
  • res/values-v21/styles.xml

    <style name="FinalStyle" parent="BaseStyle">
        <item name="specificColor">@color/secondary_color</item>
        <item name="specificText">v21</item>
    </style>
    
  • res/drawable/icon.png Common icon

  • res/drawable-v21/icon.png v21 icon

Android FinalStyle v21, FinalStyle res/values-v21 BaseStyle. , Android @drawable/icon.

+8

styles.xml ( ) .

How to remove repeating of similar styles in v19/styles.xml and v21/styles.xml files fooobar.com/questions/10758878/...

0

All Articles