![](/img/trans.png)
[英]Android, Change App:theme value of SwitchCompat programmatically
[英]How to change app android theme with switchCompat
我想用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動態更改的主題
在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);
}
重要說明:在基本活動中創建此方法。 應用主題后,您必須重新創建活動以反映更改。
嘗試這個 :
@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.