如何使用switchCompat更改app android主题

问题描述 投票:0回答:2

我想用SwithCompat动态更改我的应用程序的主题,所以我实现了这个:

public class SettingsActivity extends AppCompatActivity {

    private final static int THEME_LIGHT = 2;
    private final static int THEME_DARK = 1;

    @BindView(R.id.summary)
    TextView summary;
    @BindView(R.id.themeModeSwitchCompat)
    SwitchCompat themeMode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        SharedPreferences sharedPreferences = getSharedPreferences("VALUES", MODE_PRIVATE);
        int theme = sharedPreferences.getInt("THEME", 2);
        switch (theme) {
            case 1:
                setTheme(R.style.CustomStyle_DarkTheme);
                break;
            case 2:
                setTheme(R.style.CustomStyle_LightTheme);
                break;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        ButterKnife.bind(this);

        themeMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                summary.setText(getResources().getString(R.string.night_mode_on_summary));
                sharedPreferences.edit().putInt("THEME",1).apply();
            } else {
                summary.setText(getResources().getString(R.string.night_mode_off_summary));
                sharedPreferences.edit().putInt("THEME",2).apply();
            }
        });

    }}

我想知道为什么主题不会改变,以及如何更正我的代码以获得一个使用switchCompat动态更改的主题

android sharedpreferences android-theme
2个回答
0
投票

在xml中创建两个不同的主题。您甚至可以创建两个不同的xml文件。像RedTheme.xml和GreenTheme.xml。创建风格时不要忘记将父设置为“AppTheme”。

<resources>

<style name="RedTheme" parent="AppTheme">
    <item name="colorPrimary">@color/colorRed</item>
    <item name="colorPrimaryDark">@color/colorGreen</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.REd</item>
</style>

现在调用setOnCheckedChangeListener上的setTheme()方法,这是在运行时更改主题的本机方法。

if(isChecked)
{                
 setTheme(R.style.RedTheme);
} 
else{
 setTheme(R.style.GreenTheme);
}

重要说明:在基本活动中创建此方法。应用主题后,您必须重新创建活动以反映更改。


0
投票

试试这个 :

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  initViews()
}

private void initViews() {


    SharedPreferences sharedPreferences = getSharedPreferences("VALUES", MODE_PRIVATE);
    int theme = sharedPreferences.getInt("THEME", 2);
    switch (theme) {
        case 1:
            setTheme(R.style.CustomStyle_DarkTheme);
            break;
        case 2:
            setTheme(R.style.CustomStyle_LightTheme);
            break;
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    ButterKnife.bind(this);
    themeMode.setOnCheckedChangeListener(null);
    switch (theme) {
        case 1:
            themeMode.setChecked(true);
            break;
        case 2:
            themeMode.setChecked(false);
            break;
    }
    themeMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if (isChecked) {
            summary.setText(getResources().getString(R.string.night_mode_on_summary));
            sharedPreferences.edit().putInt("THEME", 1).apply();
            //Call the onCreate here again to apply the theme again.
            initViews();
        } else {
            summary.setText(getResources().getString(R.string.night_mode_off_summary));
            sharedPreferences.edit().putInt("THEME", 2).apply();
            //Call the onCreate here again to apply the theme again.
            initViews();
        }

    });

}
© www.soinside.com 2019 - 2024. All rights reserved.