Given:

Own topic, which inherits one of the standard ones, redefines some attributes and defines its own. Assigned to the application theme in the manifest.

Task:

Create a theme for another API so that it contains all the properties of an existing theme and redefines its attributes (for example, a theme for API> 21 , with the android:navigationBarColor attribute) that cannot be specified in themes for a lower API level.

Possible Solution:

  1. It is trivial to copy the theme definition into the values-API_LEVEL/styles.xml and add the necessary attributes. Confused by the need to edit many files when changing the attributes common to all APIs .
  2. Redefine your topic in a topic with a different name and assign it programmatically. Also an option, but I do not want to fence an extra code.

Question:

How can it be possible in the values-API_LEVEL/styles.xml to somehow redefine the theme so that it has the name identical to the theme name from values/styles.xml , values/styles.xml all the properties of this theme and defines new ones?

    1 answer 1

    We do this:

    1. Create a common theme from values/styles.xml from which we will later inherit

       <style name="Base.MyTheme" parent="Theme.AppCompat.Light.NoActionBar" > <!--определяем здесь атрибуты общие для всех API--> </style> <style name="MyTheme" parent="Base.MyTheme" > <!--тут ничего писать не надо, всё уже сделано выше--> </style> 
    2. In values-v21/styles.xml create a theme with the same name and the same parent, inherit all its properties in this way and add new ones

       <style name="MyTheme" parent="Base.MyTheme"> <item name="android:navigationBarColor">@color/material_grey_500</item> </style> 
    3. In the manifest, we indicate to the application the topic MyTheme:

       android:theme="@style/MyTheme" 
    • with all due respect - I use the eclipce development environment, so here is exactly what you wrote it automatically does when creating a new project. Values folders for sdk versions can only vary - s_klepcha
    • Um, really ... Tolley confused me with AndroidStudio, or if I confused myself ... =) - Yuriy SPb