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

时间:2015-04-25 00:02:17

标签: 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>

然后在 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>

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

1 个答案:

答案 0 :(得分:2)

这个问题很老但是有可能: 以下代码段来自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>

如果你想继承(例如)相同的风格并专注于API&gt; = 23,你必须定义一个值/ style-23.xml,如下所示:

<style name="AppTheme.AppTheme23" >
    <item name="android:windowLightStatusBar">true</item>
</style>

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

android:theme="@style/AppTheme"

这就是所有人!