在运行时使用 setTheme() 只会改变文本颜色

Using setTheme() at runtime only changes text color

我正在尝试为我的应用实施深色主题。用户可以在选项菜单中轻松地在正常和黑暗之间切换——效果很好。但是在运行时更改主题时,只有文本颜色发生变化,我不知道为什么。

我在 styles.xml 中的深色主题:

<style name="Dark" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/dark_background</item>
    <item name="colorPrimaryDark">@color/dark_top</item>
    <item name="colorAccent">@color/dark_button</item>
    <item name="colorButtonNormal">@color/dark_button</item>
    <item name="android:colorBackground">@color/dark_background</item>
    <item name="android:itemBackground">@color/dark_background</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#EAEAEA</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:textColorSecondary">@color/white</item>
    <item name="android:textColorTertiary">@color/white</item>
</style>

我的样式设置方式:

setTheme(R.style.Dark);

更改主题之前: before

更改主题后: after

我真的不知道为什么。是因为 NavigationView?

确保在 setContentView() 或扩充视图之前调用 setTheme()。根据 documentation,在上下文中实例化任何视图之前,您必须使用 setTheme()。使用 recreate() 创建 activity 的新实例,以便您可以在 onCreate() 方法中应用更改的主题。

如果你稍微搜索一下,你可以找到几个主题切换的例子。这是一个这样的例子的 link: https://gist.github.com/alphamu/f2469c28e17b24114fe5

我使用 PreferenceManager 来存储这样的设置,以便在我有多个 activity 需要使用该设置时方便地访问。除非您已经有更好的方法来存储用户的主题选择,否则我建议您使用以下示例。

示例 MyAppPreferences class:

public class MyAppPreferences {

    private static SharedPreferences getPrefs(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static int getThemeId(Context context, int defaultThemeId) {
        return getPrefs(context).getInt("CurrentThemeId", defaultThemeId);
    }
    public static void setThemeId(Context context, int value) {
        getPrefs(context).edit().putInt("CurrentThemeId", value).commit();
    }

}

示例 Activity class 使用 MyAppPreferences class:

public class MyActivity extends AppCompatActivity implements OnClickListener {

    private Button btnDark;
    private Button btnLight;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);            
        // Set the theme
        // If there is nothing set, the light theme will be used by default
        setTheme(MyAppPreferences.getThemeId(this, R.style.Light));
        setContentView(R.layout.myLayout);

        btnDark = (Button) this.findViewById(R.id.viewbtnDark);
        btnDark.setOnClickListener(this);

        btnLight = (Button) this.findViewById(R.id.viewbtnLight);
        btnLight.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // 1. Set the theme preference
        // 2. Recreate the activity to "apply" the theme
        if (v.equals(btnDark)) {
            MyAppPreferences.setThemeId(this, R.style.Dark);
            this.recreate();
        } else if (v.equals(btnLight)) {
            MyAppPreferences.setThemeId(this, R.style.Light);
            this.recreate();
        }
    }

}

您的示例主题未显示 windowActionBar 或 windowNoTitle 设置,因此如果您碰巧使用默认主题并且未在深色主题中以相同方式设置这些选项,您仍可能会遇到崩溃。检查 Logcat 是否存在这样的错误:java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor..

深色主题示例

<style name="Dark" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/dark_background</item>
    <item name="colorPrimaryDark">@color/dark_top</item>
    <item name="colorAccent">@color/dark_button</item>
    <item name="colorButtonNormal">@color/dark_button</item>
    <item name="android:colorBackground">@color/dark_background</item>
    <item name="android:itemBackground">@color/dark_background</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#EAEAEA</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:textColorSecondary">@color/white</item>
    <item name="android:textColorTertiary">@color/white</item>
</style>