我花了一天的时间来解决这个问题。除了这个问题,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](https://istack.dev59.com/SCAoB.webp)
在模拟器上仍然显示Appbar,但在真实设备上没有问题。以下实现适用于所有Android版本。
注意:我的应用在三星A51上看起来像上面的图片。我正在调查并尝试找到解决方案。
values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<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>
<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) {
loadBeforeSplash();
splashScreen = SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}