父样式取决于构建变体

问题描述 投票:1回答:2

我在themes.xml中定义了两个主题。

这两个主题通过productFlavors应用于清单中。

build.gradle

productFlavors {
            flavor1 {
                dimension "theme"
                manifestPlaceholders = [
                        appTheme: "@style/Theme1"
                ]
            }

            flavor2 {
                dimension "theme"
                manifestPlaceholders = [
                        appTheme: "@style/Theme2"
                ]
            }
    }

manifest.xml

 <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan"
            android:theme="${appTheme}" >

这可以正常工作,具体取决于所选的构建变体,将应用不同的主题。

现在我要集成Stripe SDK,巫婆声明了一个名为StripeDefaultTheme的样式。

以这种风格,我想用我自己的主题的值覆盖colorPrimarycolorAccent

是否可以声明由Build Variant确定的样式的父项?

android android-styles android-build-flavors
2个回答
0
投票

[Theme1和Theme2可能具有通用的BaseTheme,您可以在flavor res dir styles.xml文件中覆盖它。


0
投票

除了制作两个单独的主题,您还可以只拥有一个根据不同口味而变化的主题。为此,您只需要覆盖相应风味的源集中的styles.xml文件即可。例如,

  1. 主源集中的AppTheme应该只包含与条带相关的主题,

    {app_module_dir} /src/main/res/values/styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Stripe related values here. -->
    </style>
    

  2. 将Theme1的内容复制到flavor1的源集中

    {app_module_dir} /src/flavor1/res/values/styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Theme1 contents here. -->
    </style>
    

  3. 将Theme2的内容复制到flavor2的源集中

    {app_module_dir} /src/flavor2/res/values/styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Theme2 contents here. -->
    </style>
    

Mannifest

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustPan"
    android:theme="@style/AppTheme" >

build.gradle

productFlavors {
        flavor1 {
            dimension "theme"
        }

        flavor2 {
            dimension "theme"
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.