如何更新我的项目以创建多主题应用

Pie*_*ier 5 android themes

我想做一个SettingsActivity让用户个性化应用外观的应用程序。在此活动中,用户可以选择将应用程序保持在“ 浅色主题 ”(例如,带有黑色文本的白色背景)或“ 深色主题 ”,浅色主题的相反颜色有利于夜间使用。

怎么做?

我正在考虑为每个主题在xml中创建不同的布局。

编辑

下图是的示例SettingsActivity,我想更改整个应用的外观,而不是单个活动的外观。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Sul*_*n19 5

这就是我为我的应用程序所做的。我相信我的方法会有所帮助。

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>
Run Code Online (Sandbox Code Playgroud)

既然你控制通过优先主题的变化,在你注册一个首选项更改侦听PreferenceFragment

PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
Run Code Online (Sandbox Code Playgroud)

实现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.
        } 
    }
Run Code Online (Sandbox Code Playgroud)

如果后退导航导致MainActivity,请务必在onResume() 中重新创建您的MainActivity。


此外,您必须在每个活动中检查当前主题,在onCreate() 中调用super()之前。

  isThemeDark = setDarkTheme(this);
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

以下是我的应用Newslet 中夜间模式的工作原理:

在此处输入图片说明

更新: Android 现在通过其 AppCompat DayNight 主题正式支持夜间模式。这是一个相同的教程