Android 按版本应用主题

Android applying theme by version

我正在开发一个以列表视图开头的应用程序。 我在为应用程序设置主题时遇到了麻烦。 Material设计可应用于Android5.0及以上。所以我创建了一个 values-21 文件夹并创建 styles.xml 如下:

<resources>
  <!-- Inherit from the light Material Theme -->
  <style name="MyCustomTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">@color/My_LightBlue</item>
  </style>
</resources>

在 Android 5.0 版本下,此主题未应用,因为它不读取 values-21 文件夹。 我还必须在值标准文件夹中创建 styles.xml 吗? 必须创建一个新主题来更改应用栏的颜色(这是我的objective)。

我有点困惑... 谢谢

来自https://developer.android.com/training/material/compatibility.html

You can configure your app to use the material theme on devices that support it and revert to an older theme on devices running earlier versions of Android:

  1. Define a theme that inherits from an older theme (like Holo) in res/values/styles.xml.
  2. Define a theme with the same name that inherits from the material theme in res/values-v21/styles.xml.
  3. Set this theme as your app's theme in the manifest file.

如果您根据 material 设计指南设计的布局不使用 Android 5.0 中引入的任何新 XML 属性(API 级别 21),它们将在 Android 的早期版本上工作。否则,您可以提供替代布局。您还可以提供替代布局来自定义您的应用程序在早期版本 Android.

上的外观

res/layout-v21/ 中为 Android 5.0(API 级别 21)创建布局文件,在 [=11= 中为 Android 的早期版本创建替代布局文件].例如,res/layout/my_activity.xml 是 res/layout-v21/my_activity.xml.

的替代布局

检查this answer and blog