闪屏界面API在Android 12中显示操作栏

7

启动闪屏页面后,我得到了一个操作栏,但是我已经将postSplashScreenTheme设置为没有操作栏的主题。这只发生在Android 12模拟器上。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/white</item>

        <item name="windowSplashScreenAnimatedIcon">@drawable/splash_icon</item>
        <item name="postSplashScreenTheme">@style/Theme.Design.NoActionBar</item>
        <item name="windowSplashScreenIconBackgroundColor">@color/blue</item>
        <item name="windowSplashScreenAnimationDuration">800</item>

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

1
你找到解决方案了吗?@kunal-kalwar - Mihodi Lushan
是的 @MihodiLushan,现在我正在我的活动中调用这个 actionBar?.hide() 在 onCreate 中。 - Kunal Kalwar
4个回答

8

运行得非常完美!! - Saurabh Padwekar
对于那些遇到此问题并尝试此解决方案的人,需要注意的是,在调用setContent{......}之前必须先调用它。 - Sourav

2

我在onCreate中调用actionBar?.hide(),看起来按预期工作。

actionBar?.hide()

不适用于我。 - badadin

0

我花了一天的时间来解决这个问题。除了这个问题,Android Studio无法在Android 12及以上版本上运行应用程序。我不得不手动运行它。我发现问题是由模拟器引起的。

当我在真实设备上测试应用程序时,我注意到AppBar没有出现。但我发现另一个问题。在Android 9设备上,启动屏幕不会出现,但在Android 12设备上使用实现'androidx.core:core-splashscreen:1.0.0-beta02'可以完美地工作。

This white bar shown only Emulator with AndroidVersion >= 12

在模拟器上仍然显示Appbar,但在真实设备上没有问题。以下实现适用于所有Android版本。

注意:我的应用在三星A51上看起来像上面的图片。我正在调查并尝试找到解决方案。

values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Theme for MainActivity and its fragments -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/black_transparent</item>
    </style>

    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">#ffffff</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/logo_splash</item>
        <item name="windowSplashScreenAnimationDuration">200</item>
        <item name="android:windowSplashScreenBehavior" tools:targetApi="tiramisu">@drawable/logo_splash</item>

        <!-- Set the theme of the Activity that directly follows your splash screen. -->
        <item name="postSplashScreenTheme">@style/AppTheme</item>
    </style>
</resources>

main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
        <application
            android:name=".MyApplication"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.App.Starting">
    
            <activity
                android:name=".MainActivity"
                android:exported="true"
                android:launchMode="singleTop"
                android:theme="@style/Theme.App.Starting">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".OtherActivity"
                android:parentActivityName=".MainActivity"
                android:screenOrientation="portrait"
                android:theme="@style/AppTheme"/>
        </application>
</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {
        protected void onCreate(Bundle savedInstanceState) {
            // Load things that should be loaded before rendering MainActivity.
            loadBeforeSplash();
    
            // Handle the splash screen transition.
            splashScreen = SplashScreen.installSplashScreen(this);
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Other MainActivity stuffs
    }
}

0
如果您正在使用Androidx SplashScreen compat library,请将此代码添加到您的启动屏幕主题中。
   <item name="postSplashScreenTheme">@style/Theme.App</item>

例如:

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
   <!-- Set the splash screen background, animated icon, and animation duration. -->
   <item name="windowSplashScreenBackground">@color/...</item>

   <!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
        animated drawable. One of these is required. -->
   <item name="windowSplashScreenAnimatedIcon">@drawable/...</item>
   <!-- Required for animated icons -->
   <item name="windowSplashScreenAnimationDuration">200</item>

   <!-- Set the theme of the Activity that directly follows your splash screen. -->
   <!-- Required -->
   <item name="postSplashScreenTheme">@style/Theme.App</item>
</style>

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