简体   繁体   English

如何使用switchCompat更改app android主题

[英]How to change app android theme with switchCompat

I want dynamically change the theme of my app with SwithCompat, so I implemented this: 我想用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();
            }
        });

    }}

I want to know why the theme doesn't change, and how can i correct my code to get a theme that change dynamically with switchCompat 我想知道为什么主题不会改变,以及如何更正我的代码以获得一个使用switchCompat动态更改的主题

Create two different theme in xml. 在xml中创建两个不同的主题。 You can even create two different xml files . 您甚至可以创建两个不同的xml文件。 Like RedTheme.xml and GreenTheme.xml . 像RedTheme.xml和GreenTheme.xml。 While create the style dont forget to set the parent as "AppTheme". 创建风格时不要忘记将父设置为“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>

Now on call setTheme() method on setOnCheckedChangeListener , native method to change the theme at run time. 现在调用setOnCheckedChangeListener上的setTheme()方法,这是在运行时更改主题的本机方法。

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

Important : Create this method in your base activity . 重要说明:在基本活动中创建此方法。 Once you apply the theme , you have to re-create your activity to reflect the changes. 应用主题后,您必须重新创建活动以反映更改。

Try This : 尝试这个 :

@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();
        }

    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM