确保您在调用setTheme() 之前调用setContentView() 或膨胀视图。根据documentation,您必须在上下文中实例化任何视图之前使用setTheme()。使用 recreate() 创建一个新的 Activity 实例,以便您可以在 onCreate() 方法中应用更改后的主题。
如果您稍微搜索一下,您可以找到几个主题切换的示例。这是一个此类示例的链接:
https://gist.github.com/alphamu/f2469c28e17b24114fe5
我使用PreferenceManager 来存储此类设置,以便在我有多个活动需要使用该设置时轻松访问。除非您已经有更好的方法来存储用户的主题选择,否则我建议您使用以下示例。
MyAppPreferences 类示例:
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();
}
}
使用 MyAppPreferences 类的示例 Activity 类:
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>