You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

系统浅色主题下App深色主题ActionBar加载异常问题及修复

Android主题切换ActionBar显示异常修复

问题描述

当系统主题为浅色、App内设置为深色主题时,MainActivity启动时ActionBar元素显示为深色(不符合预期);但从App设置页面重新加载深色主题后,ActionBar元素会正常显示为浅色。该问题仅出现在MainActivity中。

相关代码

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
                PreferenceUtils.setTheme(this);
                setContentView(R.layout.activity_main);
                if (getSupportActionBar() != null) {
                    this.getSupportActionBar().setElevation(0);
                }
                setTitle("Sample App");
                MobileAds.initialize(this, initializationStatus -> {});
                FrameLayout adContainerView = findViewById(R.id.ad_view);
                AdsUtils.loadBannerAd(this, adContainerView);
    
            drawerLayout = findViewById(R.id.drawer_layout);
            drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_drawer_open, R.string.nav_drawer_close);
            drawerLayout.addDrawerListener(drawerToggle);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            drawerToggle.syncState();
    
            navigationView = findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
                .
                .
                .

PreferenceUtils.java

public class PreferenceUtils {

    public static final String LIGHT_MODE = "light";
    public static final String DARK_MODE = "dark";

    public static void setTheme(Activity activity) {
        switch (PreferenceManager.getDefaultSharedPreferences(activity).getString("pref_key_theme"), "")) {
            case LIGHT_MODE: {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            } case DARK_MODE: {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
            } default: {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
                }
                break;
            }
        }
    }
}

activity_main.xml

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <RelativeLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:tabIndicatorColor="?attr/colorAccent"
            app:tabSelectedTextColor="?android:textColorPrimary"
            app:tabTextColor="?android:textColorSecondary"
            app:tabMode="fixed" />
     
    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SampleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/red_800</item>
        <item name="colorPrimaryVariant">@color/red_900</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/red_700</item>
        <item name="colorSecondaryVariant">@color/red_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

themes-night.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SampleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/dark_grey_900</item>
        <item name="colorPrimaryVariant">@color/black</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/blue_200</item>
        <item name="colorSecondaryVariant">@color/blue_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/Theme.SampleApp">         
        <activity android:name=".MainActivity" android:configChanges="locale|orientation|screenSize|keyboardHidden|uiMode" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

修复方案(2022.10.30更新)

将MobileAds.initialize(this, initializationStatus -> {});代码移至onCreate方法最顶部后,问题已解决。修改后的代码如下:

protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    MobileAds.initialize(this, initializationStatus -> {});
                    PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
                    PreferenceUtils.setTheme(this);
                    setContentView(R.layout.activity_main);
                    if (getSupportActionBar() != null) {
                        this.getSupportActionBar().setElevation(0);
                    }
                    setTitle("Sample App");

内容的提问来源于stack exchange,提问作者TheAnswer042

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 08:45:51