刷新设置片段以合并深色模式
Refreshing Settings Fragment to Incorporate Dark Mode
我有一个应用程序,我在其中使用 AppCompatDelegate 和 values-night.xml 以编程方式在应用程序内切换到深色主题。我有一个带有 SettingsFragment 的 SettingsActivity.java,我在其中设置了一个 OnSharedPreferenceChangeListener 来监听相应的 SharedPreferences 值。在“设置”页面中,我包含了暗模式设置。但是,在切换设置时,暗模式不会自动打开;相反,我必须退出应用程序的 SettingsActivity 才能更改模式。
这是我的代码:
SettingsActivity.java
public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
toolbar = findViewById(R.id.toolbar_settings);
setSupportActionBar(toolbar);
if (findViewById(R.id.fragment_container)!=null) {
if (savedInstanceState!=null) {
return;
}
getFragmentManager().beginTransaction().add(R.id.fragment_container, new SettingsFragment()).commit();
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.settings_menu, menu);
return true;
}
public void savePrefs () {
SharedPreferences sharedPreferences = getSharedPreferences("Shared_Preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
}
}
SettingsFragment.java
public class SettingsFragment extends PreferenceFragment {
public int currTheme;
public static final String theme = "theme";
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private ViewGroup parent;
private View currView;
@Override
public void onCreate(@Nullable Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
addPreferencesFromResource(R.xml.preferences);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Preference preference = findPreference(s);
if (s.equals(theme)) {
currTheme = Integer.valueOf(sharedPreferences.getString(s, "0"));
switch (currTheme) {
case 2 :
preference.setSummary("Dark");
break;
case 1 :
preference.setSummary("Light");
break;
default:
preference.setSummary("System Settings");
}
preference.setTitle("Theme");
} ...
}
};
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
parent = container;
currView = view;
view.setBackgroundColor(ContextCompat.getColor(parent.getContext(), R.color.colorPrimary));
return view;
}
@Override
public void onResume() {
super.onResume();
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);
...
preferenceTheme.setTitle("Theme");
int themeSet = Integer.valueOf(getPreferenceScreen().getSharedPreferences().getString(theme, "System Settings"));
switch (themeSet) {
case 2 :
preferenceTheme.setSummary("Dark");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case 1 :
preferenceTheme.setSummary("Light");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
default :
preferenceTheme.setSummary("System Default");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
currView.setBackgroundColor(ContextCompat.getColor(parent.getContext(), R.color.cpWhite));
parent.getContext().setTheme(R.style.PreferenceTheme);
}
@Override
public void onStart() {
super.onStart();
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
}
}
当 运行 时,片段确实记录了主题的变化,但它没有 改变 主题。
这是怎么回事:
深色主题集:
更改为浅色主题:
首选项设置为浅色主题但主题未设置为浅色:
这个问题有解决办法吗?
您似乎没有在首选项更改侦听器中包含任何 theme-setting 逻辑(仅在 onResume 中)。
尝试在您的听众中加入 AppCompatDelegate.setDefaultNightMode(...)
。
我有一个应用程序,我在其中使用 AppCompatDelegate 和 values-night.xml 以编程方式在应用程序内切换到深色主题。我有一个带有 SettingsFragment 的 SettingsActivity.java,我在其中设置了一个 OnSharedPreferenceChangeListener 来监听相应的 SharedPreferences 值。在“设置”页面中,我包含了暗模式设置。但是,在切换设置时,暗模式不会自动打开;相反,我必须退出应用程序的 SettingsActivity 才能更改模式。 这是我的代码:
SettingsActivity.java
public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
toolbar = findViewById(R.id.toolbar_settings);
setSupportActionBar(toolbar);
if (findViewById(R.id.fragment_container)!=null) {
if (savedInstanceState!=null) {
return;
}
getFragmentManager().beginTransaction().add(R.id.fragment_container, new SettingsFragment()).commit();
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.settings_menu, menu);
return true;
}
public void savePrefs () {
SharedPreferences sharedPreferences = getSharedPreferences("Shared_Preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
}
}
SettingsFragment.java
public class SettingsFragment extends PreferenceFragment {
public int currTheme;
public static final String theme = "theme";
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private ViewGroup parent;
private View currView;
@Override
public void onCreate(@Nullable Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
addPreferencesFromResource(R.xml.preferences);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Preference preference = findPreference(s);
if (s.equals(theme)) {
currTheme = Integer.valueOf(sharedPreferences.getString(s, "0"));
switch (currTheme) {
case 2 :
preference.setSummary("Dark");
break;
case 1 :
preference.setSummary("Light");
break;
default:
preference.setSummary("System Settings");
}
preference.setTitle("Theme");
} ...
}
};
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
parent = container;
currView = view;
view.setBackgroundColor(ContextCompat.getColor(parent.getContext(), R.color.colorPrimary));
return view;
}
@Override
public void onResume() {
super.onResume();
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);
...
preferenceTheme.setTitle("Theme");
int themeSet = Integer.valueOf(getPreferenceScreen().getSharedPreferences().getString(theme, "System Settings"));
switch (themeSet) {
case 2 :
preferenceTheme.setSummary("Dark");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case 1 :
preferenceTheme.setSummary("Light");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
default :
preferenceTheme.setSummary("System Default");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
currView.setBackgroundColor(ContextCompat.getColor(parent.getContext(), R.color.cpWhite));
parent.getContext().setTheme(R.style.PreferenceTheme);
}
@Override
public void onStart() {
super.onStart();
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
}
}
当 运行 时,片段确实记录了主题的变化,但它没有 改变 主题。 这是怎么回事:
深色主题集:
更改为浅色主题:
首选项设置为浅色主题但主题未设置为浅色:
这个问题有解决办法吗?
您似乎没有在首选项更改侦听器中包含任何 theme-setting 逻辑(仅在 onResume 中)。
尝试在您的听众中加入 AppCompatDelegate.setDefaultNightMode(...)
。