如何让我的主题以编程方式转移到其他活动中?

How do I get my theme to programmatically get carried over into other activities?

我在我的应用程序的设置页面中创建了一个功能,其中包含一个开关 - 当按下时 - 切换到辅助 "Night" 主题。我大部分时间都关注 this tutorial。但是,我不知道如何将这种夜间模式应用到我的其他活动中?我试过在我的 activity 中调用 "if switch checked",但它显然没有看到那个开关。我主要想知道的是,如何查看另一个activity中的开关状态?这是正确的做法吗?如果我错过了这个问题中的任何其他内容,请告诉我。

// ======== 设置页面代码 ======== //

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);


    // ======== Night Mode ======== //
    SwitchCompat switchCompat;
    final SharedPref sharedPref;

    sharedPref = new SharedPref(this);

    if (sharedPref.loadNightModeState()) {
        setTheme(R.style.AppTheme_Night);
        getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
        actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
    } else setTheme(R.style.AppTheme);

    setContentView(R.layout.activity_settings);
    switchCompat = (SwitchCompat) findViewById(R.id.night_switch);
    if (sharedPref.loadNightModeState()) {
        switchCompat.setChecked(true);
    }

    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
               sharedPref.setNightModeState(true);
                restartApp();
            } else {
                sharedPref.setNightModeState(false);
                restartApp();
            }
        }
    });
}




private void restartApp() {
    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
    startActivity(intent);
    finish();
}

// ======== SharedPref CODE ======== //

public class SharedPref {

private SharedPreferences sharedPreferences;


public SharedPref(Context context) {
    sharedPreferences = context.getSharedPreferences("filename", Context.MODE_PRIVATE);
}


public void setNightModeState(Boolean state) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("NightMode",state);
    editor.apply();
}

public Boolean loadNightModeState (){
    Boolean state = sharedPreferences.getBoolean("NightMode", false);
    return state;
}

在你的应用程序 class 里面 onCreate

SharedPreferences sharedPreferences = getSharedPreferences("Your_Shared_pref", Context.MODE_PRIVATE);
boolean nightMode = sharedPreferences.getBoolean(SettingsActivity.DARK_THEME_PREFERENCE_KEY, false);

AppCompatDelegate.setDefaultNightMode(nightMode ? MODE_NIGHT_YES : MODE_NIGHT_NO);

比起你的 activity,这样做:

 switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        AppCompatDelegate.setDefaultNightMode(isChecked ? MODE_NIGHT_YES : MODE_NIGHT_NO);
        if (isChecked) {
           sharedPref.setNightModeState(true);
            recreate();
        } else {
            sharedPref.setNightModeState(false);
            recreate();
        }
    }
});

@Override
public void recreate() {
    finish();
    overridePendingTransition(R.anim.anime_fade_in,
            R.anim.anime_fade_out);
    startActivity(getIntent());
    overridePendingTransition(R.anim.anime_fade_in,
            R.anim.anime_fade_out);
}

你可以在网上找到动画xml。

我把它放在我的 onCreate 和我的 super.onCreate 之间。我使用的代码取自我的设置 java 页面。事实证明,这很容易理解,我只是需要有人来透视它!

 @Override
    protected void onCreate(Bundle savedInstanceState) {


        final SharedPref sharedPref;
        sharedPref = new SharedPref(this);

        if (sharedPref.loadNightModeState()) {
            setTheme(R.style.AppTheme_Night);
            //restartApp();
            //getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
            //actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
        } else setTheme(R.style.AppTheme);
        //restartApp();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);