【发布时间】:2016-12-04 17:29:09
【问题描述】:
我想创建一个SettingsActivity 让用户个性化应用程序的外观。
在此活动中,用户可以选择将应用程序保持在“浅色主题”(这意味着例如带有黑色文本的白色背景)或“深色主题”中,相反颜色的浅色主题有利于夜间使用。
怎么可能呢?
我正在考虑在 xml 中为每个主题创建不同的布局。
编辑
下面的图片是SettingsActivity 的示例,我想更改整个应用程序的外观,而不是单个活动。
【问题讨论】:
我想创建一个SettingsActivity 让用户个性化应用程序的外观。
在此活动中,用户可以选择将应用程序保持在“浅色主题”(这意味着例如带有黑色文本的白色背景)或“深色主题”中,相反颜色的浅色主题有利于夜间使用。
怎么可能呢?
我正在考虑在 xml 中为每个主题创建不同的布局。
编辑
下面的图片是SettingsActivity 的示例,我想更改整个应用程序的外观,而不是单个活动。
【问题讨论】:
这就是我为我的应用程序所做的。我相信我的方法会有所帮助。
在 styles.xml 中设置您的 Light 和 Dark 主题,如下所示:
<!-- Use this theme in Manifest by default -->
<style name="MyLightTheme" parent="Base.AppTheme.Light"></style>
<!-- Base light theme containing all styles -->
<style name="Base.AppTheme.Light" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
... Other styles
</style>
<!-- Use this to switch to Dark theme -->
<style name="MyDarkTheme" parent="Base.AppTheme.Dark"></style>
<!-- Base dark theme containing all styles -->
<style name="Base.AppTheme.Dark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
... Other styles
</style>
由于您通过首选项控制主题更改,因此请在您的 PreferenceFragment 中注册一个首选项更改侦听器。
PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
实现onSharedPreferenceChanged():
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getString(R.string.pref_key_nighttheme))) {
if (sharedPreferences.getBoolean(getString(R.string.pref_key_nighttheme), false)) {
// Night theme enabled
getActivity().setTheme(R.style.MyDarkTheme);
getActivity().getApplication().setTheme(R.style.MyDarkTheme);
darkTheme = true;
} else {
getActivity().setTheme(R.style.MyLightTheme);
getActivity().getApplication().setTheme(R.style.MyLightTheme);
darkTheme = false;
}
getActivity().recreate(); // This is important. It allows the theme change to take effect.
}
}
如果 Back Navigation 导致 MainActivity,请务必在 onResume() 中重新创建您的 MainActivity。
此外,您必须在 onCreate() 中调用 super() 之前检查每个 Activity 中的当前主题。
isThemeDark = setDarkTheme(this);
setDarkTheme() 是我创建的一个助手,它通过 SharedPreference 检查当前主题。它检查是否需要更改主题。
public static boolean setDarkTheme(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean isDarkTheme = prefs.getBoolean(context.getString(R.string.pref_key_nighttheme), false);
context.setTheme(SettingsActivity.darkTheme ? R.style.MyDarkTheme : R.style.MyLightTheme);
return isDarkTheme;
}
这是夜间模式在我的应用程序Newslet 中的工作方式:
更新: Android 现在通过其 AppCompat DayNight Theme 正式支持夜间模式。 这是一个tutorial 。
【讨论】:
您可以创建自己的主题,然后当用户想要更改它们时,将此代码添加到您的活动中,您可以选择任何您想要的主题,而不仅仅是 Holo。
getApplication().setTheme(Theme.Holo)
【讨论】:
也许这个答案可以帮助你解决这个问题 https://stackoverflow.com/a/4673209/4858673
public class BaseActivity extends Activity {
protected void onCreate(Bundle state) {
super.onCreate(state);
String theme = PreferenceManager.getDefaultSharedPreferences(this).getString("theme", "black");
setTheme(getTheme(theme);
}
private int getTheme(String theme) {
if (theme.equals("black") return R.style.ThemeBlack;
if (theme.equals("white") return R.style.ThemeWhite;
...
return android.R.style.Theme; // stub
}
}
并从此 BaseActivity 扩展您的所有活动。如果您想使用 PreferenceActivity 或 PreferenceFragment,请将这段代码放入您的实现中,这样您就可以只扩展一个类 - Activity (AppCompatActivity) 或 PreferenceActivity
附: R.style.ThemeBlack 是您在styles.xml 中的主题设置
<style name="ThemeBlack" parent="android:Theme.Holo">
</style>
【讨论】:
首先,您需要以 xml 样式创建一些定义明确的主题。
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primaryColorAmber</item>
<item name="colorPrimaryDark">@color/primaryDarkColorAmber</item>
<item name="colorAccent">@color/secondaryColorAmber</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.RED" parent="AppTheme.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primaryColorRed</item>
<item name="colorPrimaryDark">@color/primaryDarkColorRed</item>
<item name="colorAccent">@color/secondaryColorRed</item>
</style>
<style name="AppTheme.PINK" parent="AppTheme.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primaryColorPink</item>
<item name="colorPrimaryDark">@color/primaryDarkColorPink</item>
<item name="colorAccent">@color/secondaryColorPink</item>
</style>
要在运行时更改主题,请在基本活动 onCreate() 方法中和 setContentView() 之前使用以下代码。
// 要更改主题,只需输入您的主题 ID。
int theme = getThemeFromPreferences(); // R.style.AppTheme_RED
setTheme(theme);
要更改设置/偏好活动的主题(从您更改主题的位置),您需要通过调用该活动的以下方法来重新创建该活动。
//优先存储您的主题ID
saveThemeInPreferences(R.style.AppTheme_RED);
//recreate this activity to see changes
SettingActivity.this.recreate();
更多细节和示例代码... 看看 Android multi theme implementation
【讨论】: