Android 12闪屏界面API与Material组件不兼容

7

我一直在遵循文档,但不幸的是,当使用Material Component作为整个应用程序主题时,文档并未包括适应性调整。

<style name="Theme.App" parent="Theme.MaterialComponents.DayNight">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
    <item name="colorOnPrimary">@android:color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/colorPrimary</item> <!--Fab color-->
    <item name="colorOnSecondary">@color/colorAccent</item> <!--Fab icon color-->
    <item name="colorSecondaryVariant">@color/colorAccentDark</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <!--<item name="tabStyle">@style/AppTabLayout</item>-->
    <!-- The color for all other text including the menu -->
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textColorHighlight">@color/colorAccentDark</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>

    <item name="checkboxStyle">@style/AppCheckBoxStyle</item>
    <item name="popupMenuBackground">@drawable/popup_bg_rounded</item>
    <item name="materialAlertDialogTheme">@style/AppDialogTheme</item>

    <item name="autoCompleteTextViewStyle">@style/AppCursor</item>

    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">?attr/actionBarSize</item>

    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">
            @color/whitePrimaryDark
        </item>
        <item name="postSplashScreenTheme">
            @style/Theme.Cryptonian.NoActionBar
        </item>
    </style>

Android清单文件

    <activity
        android:name=".presentation.MainActivity"
        android:exported="true"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.App.Starting"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

它崩溃了并显示错误信息

  Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).

有人遇到同样的问题吗?


1
你是否在设置Activity布局内容之前调用了 installSplashScreen() 方法? 参考链接: https://developer.android.com/reference/androidx/core/splashscreen/SplashScreen.Companion#(android.app.Activity).installSplashScreen() - hiddeneyes02
2个回答

6
解决方案是在调用super.onCreate()setContentView之前安装启动画面API。

在我的情况下,在super.onCreate()之前调用它不会显示启动画面。但在super.onCreate()之后、在其他任何操作之前调用它就可以正常工作。 - undefined

1
你需要修改这个内容:
<item name="postSplashScreenTheme">@style/Theme.Cryptonian.NoActionBar</item>

转换为:

<item name="postSplashScreenTheme">@style/Theme.App</item>

postSplashScreenTheme 需要获取您的应用主题。在显示启动画面后,您的应用程序主题将被使用。


抱歉,我已经解决了这个问题,这不是解决方案。无论如何还是谢谢。 - Bitwise DEVS
谢谢,虽然这个解决方案在文档中有提到,但我不知道为什么错过了它。也许这不是 OP 的解决方案,但确实是这个错误的一个解决方案。 - rtsketo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接