在v19/v21中重复样式

use*_*382 12 android android-layout android-xml android-theme android-styles

我有这个styles.xml:

<style name="UserTheme" parent="ThemeBase">
    <item name="android:editTextStyle">@style/EditTextTheme</item>
</style>
Run Code Online (Sandbox Code Playgroud)

为什么我必须editTextStylev19/styles.xml和中重复这一行v21/styles.xml.

v21/styles.xml:

<style name="UserTheme" parent="ThemeBase">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:editTextStyle">@style/EditTextTheme</item>
</style>
Run Code Online (Sandbox Code Playgroud)

有没有办法在主要styles.xml地方调用它并让它适用于所有地方,所以我不必多次写它?

Vla*_*ski 15

我找不到任何推荐的解决方案,所以我深入研究AppCompat源代码.他们这样做的方式是这样的.

在styles.xml中

    <style name="Base.V7.Theme.YourThemeName" parent="Theme.AppCompat.Light.NoActionBar">
    </style>

    <style name="Base.Theme.YourThemeName" parent="Base.V7.Theme.YourThemeName" />

    <style name="Theme.YourThemeName" parent="Base.Theme.YourThemeName" >
       <item name="colorPrimary">@color/primary</item>
       <item name="colorPrimaryDark">@color/primary_dark</item>
       <item name="colorAccent">@color/accent</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

在你的styles-v21.xml中

    <style name="Base.V21.Theme.YourThemeName" parent="Base.V7.Theme.YourThemeName">
       <item name="android:navigationBarColor">@color/white</item>
       <item name="android:windowTranslucentStatus">true</item>
    </style>
    <style name="Base.Theme.YourThemeName" parent="Base.V21.Theme.YourThemeName" />
Run Code Online (Sandbox Code Playgroud)

在你的styles-v22.xml中

    <style name="Base.V22.Theme.YourThemeName" parent="Base.V21.Theme.YourThemeName">
       <item name="android:navigationBarColor">@color/black</item>
       <item name="android:windowTranslucentStatus">false</item>
    </style>
    <style name="Base.Theme.YourThemeName" parent="Base.V22.Theme.YourThemeName" />
Run Code Online (Sandbox Code Playgroud)

对于每个新版本,您都可以扩展以前的基本版本.如果要覆盖不同版本的任何属性,只需将其放在Base.VXX.Theme.YourThemeNamestyles-vXX.xml文件的块中即可.


Vip*_*mar 6

  1. 为什么我必须在v19 / styles.xml和v21 / styles.xml中重复editTextStyle行?

如果您对某些属性应用了STYLE,Android将在styles.xml文件中搜索api_level <= Android_device_api_level的最高api级别,并在其中搜索STYLE。如果找到,将应用该样式查看,否则将继续在较低的api级别文件中搜索该样式。

例如-如果您有四个文件styles.xml(默认),v19 / styles.xml,v21 / styles.xml,v25 / styles.xml,并且您的设备运行在api级别24上,则它将在v21中搜索STYLE /styles.xml,然后是v19 / styles.xml,最后是styles.xml(默认)。只有第一次出现的STYLE才会被应用。因此,您不能仅在特定于版本的styles.xml文件中仅定义其他属性。

如果您不想重复通用属性,则可以选择替代方法。要声明Android 5.0(API级别21)及更高版本的窗口转换,您需要使用一些新属性。因此,res / values / styles.xml中的基本主题如下所示:

<resources>
    <!-- base set of styles that apply to all versions -->
    <style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryTextColor</item>
        <item name="colorAccent">@color/secondaryColor</item>
    </style>

    <!-- declare the theme name that's actually applied in the manifest file -->
    <style name="AppTheme" parent="BaseAppTheme" />
</resources>
Run Code Online (Sandbox Code Playgroud)

然后,在res / values-v21 / styles.xml中添加特定于版本的样式,如下所示:

<resources>
<!-- extend the base theme to add styles available only with API level 21+ -->
<style name="AppTheme" parent="BaseAppTheme">
    <item name="android:windowActivityTransitions">true</item>
    <item name="android:windowEnterTransition">@android:transition/slide_right</item>
    <item name="android:windowExitTransition">@android:transition/slide_left</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在,您可以在清单文件中应用AppTheme,然后系统为每个系统版本选择可用的样式。

  1. 有没有一种方法可以在主要的styles.xml中调用它并将其应用于所有地方,所以我不必多次编写它?

是的,有一种方法只能维护一个styles.xml文件。

首先,开始使用AppCompat主题。它们提供向后兼容性,并且也适用于较早的android版本。

现在,在styles.xml(默认)文件中定义所有样式,如果您的Android Studio向您显示高级API支持的某些属性的警告/错误: 来自Andorid Studio的警告

您可以使用以下命令禁止显示该警告: tools:targetApi="SupportedAndroidVersionName" 在此处输入图片说明

现在,如果不支持该属性,Android将忽略该特定属性,并且您的整个样式对于较低和较高的api级别均可完美运行。

在此处阅读有关样式和主题的更多信息。

希望能帮助到你 :)


Kus*_*rma 1

较新版本的 Android 为应用程序提供了额外的主题,您可能希望在这些平台上运行时使用这些主题,同时仍与旧版本兼容。您可以通过自定义主题来实现此目的,该主题使用资源选择根据平台版本在不同的父主题之间切换。

为什么我必须重复and editTextStyle中的行?v19/styles.xmlv21/styles.xml

因为如果您的应用程序在 上运行v21v21/styles.xml则将被加载,如果在 上运行v19v19/styles.xml则将被加载。如果您没有 v21/styles.xml,或者v19/styles.xml应用程序将自动使用您的默认设置values/styles.xml,但您将无法利用仅为v21或提供的新功能v19

有关更多参考,您可以阅读支持不同设备根据平台版本选择主题