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

How to update my project creating a multi theme app

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

怎么做到的?

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

编辑

下面的图片是 SettingsActivity 的例子,我想改变整个应用程序的外观,而不是单个 activity。

您可以创建自己的主题,然后当用户想要更改它们时将此代码添加到您的 activity 您可以选择任何您想要的主题,而不仅仅是 Holo。

getApplication().setTheme(Theme.Holo)

也许这个答案可以帮助您解决这个问题

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
    }
}

并从这个基地扩展您的所有活动Activity。如果您想使用 PreferenceActivity 或 PreferenceFragment,请将这段代码放入您的实现中,这样您就可以只扩展一个 class - Activity (AppCompatActivity) 或 PreferenceActivity

P.S。 R.style.ThemeBlack 是您在 styles.xml

中的主题设置
<style name="ThemeBlack" parent="android:Theme.Holo">
</style>

这就是我为我的应用程序所做的。我相信我的方法可以提供帮助。

styles.xml 中设置您的明暗主题,如下所示:

     <!-- 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.
        } 
    }

确保在 onResume() 中重新创建您的 MainActivity 如果后退导航指向 MainActivity.


此外,在 onCreate()[=50 中调用 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 同样的。

  1. 首先,您需要在 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>
    
  2. 要在运行时更改主题,请在基础 activity onCreate() 方法和 setContentView() 之前使用以下代码。

    // 要更改主题,只需输入您的主题 ID。

    int theme = getThemeFromPreferences(); // R.style.AppTheme_RED
    setTheme(theme);
    
  3. 要更改 setting/preference activity 的主题(从您更改主题的位置),您需要通过调用 activity 的以下方法重新创建 activity activity.

    //在偏好中存储你的主题id

    saveThemeInPreferences(R.style.AppTheme_RED);
    //recreate this activity to see changes
    SettingActivity.this.recreate();
    

有关更多详细信息和示例代码... 看看 Android multi theme implementation