How to use device default theme for app?

前端 未结 2 2048
南方客
南方客 2020-11-28 05:53

I have an app that I want to develop for Android 2.1, 2.2, 2.3.3, 3.0/3.1 and 4.0.x. Between those API levels, there are about 3 different types of Themes. The latest 4.0.x

2条回答
  •  野性不改
    2020-11-28 06:00

    There are currently up to 3, sometimes 4 Themes available for Android devices (.Light variations and similar not included)

    Theme

    Theme

    the default for the earliest versions of Android up to 2.3 Gingerbread(10), including some minor style changes in those versions


    Theme.Holo

    Theme.Holo

    introduced with Android 3.0 Honeycomb (11)


    Theme.Material

    Theme.Material

    new in Android 5.0 Lollipop (21)


    Theme.DeviceDefault

    (Could be anything)

    introduced with 4.0 Ice Cream Sandwich (14), a theme that can be customized by the device manufacturer. It represents the native look of the device - i.e. Holo or Material on Nexus devices (& in Android Studio's design editor), maybe something custom on other devices. In case "something custom" isn't an option, those devices must come with the stock themes. Apps that want the stock theme have to specify it though.


    What is the best approach to this?

    No theme + targetSdkVersion >= 14

    The simplest, but not necessarily best option is to define no theme at all. Android will then select the default for you. But Android does not want to surprise your app with themes you're not expecting so it falls back to the Theme you probably had designed your app for. It does so by looking at the android:targetSdkVersion within AndroidManifest.xml (which can nowadays be set via gradle).

    • Apps that target old platforms, which had only Theme (i.e. API levels 3-10), will get only Theme.
    • Apps targeting 11-13 get Theme.Holo.
    • 14 or above will get Theme.DeviceDefault.

    Since this is just for backwards compatibility, you won't get Theme.Material on your old Gingerbread phone. Therefore no theme + target 14+ = device default.

    Specifying different themes

    Android's resource overlay system allows to specify styles based on device API level. For example different versions of a style in res/values-v11 and res/values-v21. This is also what any newly created apps via Android Studio will setup for you.

    As an example, the most basic setup for a .Light themed app looks like this:

    /res/values/styles.xml is applied to every device and serves as base