如何从不同api版本指定的样式继承

jwi*_*ir3 7 android android-styles

这是一个非常基本的问题 - 我支持API版本11+,我想在具有更新API版本的手机上使用功能.但是,如果我不必重新定义我的风格,那就太好了.所以,例如,如果在values/styles.xml中我有:

<resources
    xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="Theme.MyApp.Input.Text">
    <item name="android:layout_width">match_parent<item>
    <item name="android:layout_height>wrap_content</item>
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后在values-v14/styles.xml中我有:

<resources
  xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="Theme.MyApp.Input.Text">
    <item name="android:textAllCaps">true</item>
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后,在具有API 14+的设备上,我将获得两种样式的并集.

小智 3

这个问题很老了,但有可能:以下代码片段来自values/styles.xml

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/green</item>
        <item name="colorPrimaryDark">@color/dark</item>
        <item name="colorAccent">@color/green</item>
        <item name="checkboxStyle">@style/AppTheme.CheckBox</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

如果您想继承(例如)相同的样式并专注于 API >=23,则必须定义一个values/style-23.xml,如下所示:

<style name="AppTheme.AppTheme23" >
    <item name="android:windowLightStatusBar">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

显然在 AndroidManifest.xml 中要指定的主题是:

android:theme="@style/AppTheme"
Run Code Online (Sandbox Code Playgroud)

这就是大家!