Я потратил много времени на то, чтобы заставить это работать, и теперь в моем приложении есть выбираемые темы Light и Dark, которые можно даже выбирать динамически, сразу же вступая в игру, включая Prefs. Ниже приведен ресурс, который я использую, с комментариями некоторых других идей, с которыми я играл на каком-то этапе.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--The base theme ensures nothing is shown until the first activity. All activities and fragmenst must-->
<!--set a them in onCreate or in AndroidManifext or they wil crash because of no title.-->
<!--Using Theme.Holo.NoActionBar suppresses the ActionBar initially so Icon doesn't show until new theme is set.-->
<style name="MyAppThemeInitial" parent="@android:style/Theme.Holo">
<!--<item name="android:windowBackground">@color/initial_background_grey</item>-->
<!--<item name="android:actionBarStyle">@style/MyActionBar</item>-->
<item name="android:windowDisablePreview">true</item>
</style>
<style name="MyAppThemeDark" parent="@android:style/Theme.Holo">
<item name="android:windowBackground">@color/black</item>
</style>
<style name="MyAppThemeLight" parent="android:Theme.Holo.Light">
<item name="android:windowBackground">@color/white</item>
</style>
<!--<!–This is so that only the icon is showing in the intial theme–>-->
<!--<!– ActionBar styles –>-->
<!--<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">-->
<!--<item name="android:background">@android:color/transparent</item>-->
<!--<item name="android:titleTextStyle">@style/MyActionBarTextAppearance</item>-->
<!--</style>-->
<!--<style name="MyActionBarTextAppearance">-->
<!--<item name="android:textColor">@android:color/transparent</item>-->
<!--</style>-->
</resources>
Ключевым элементом здесь является
<item name="android:windowDisablePreview">true</item>
и это заняло у меня некоторое время, чтобы понять. Мое приложение выполняет тяжелую работу при запуске, поэтому было важно, чтобы обязательная тема манифеста не отображалась перед той, которую я установил в onCreate.
Чтобы иметь возможность перезапускать приложение динамически, я проверяю, было ли приложение запущено/перезапущено таким образом.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean useThemeLight = sp.getBoolean("useThemeLight", false);
//Since this Activity can also be started by the Theme Toggle in the Action bar we need to see if
//there is a TOGGLE_THEME extra which only it uses
Intent curIntent = this.getIntent();
if (curIntent.getExtras() != null && curIntent.getExtras().containsKey(TOGGLE_THEME)) {
if(curIntent.getStringExtra(TOGGLE_THEME).equals("Dark")){
this.setTheme(R.style.MyAppThemeDark);
CurrentTheme = "Dark";
}else{
this.setTheme(R.style.MyAppThemeLight);
CurrentTheme = "Light";
}
activityThemeToggleActive = true;
} else {
if (useThemeLight) {
this.setTheme(R.style.MyAppThemeLight);
CurrentTheme = "Light";
} else {
this.setTheme(R.style.MyAppThemeDark);
CurrentTheme = "Dark";
}
}
В настройках я делаю это.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean useThemeLight = sp.getBoolean("useThemeLight", false);
if (useThemeLight) {
this.setTheme(R.style.MyAppThemeLight);
} else {
this.setTheme(R.style.MyAppThemeDark);
}
Надеюсь, это поможет вам начать. С уважением, Джон.
person
jcddcjjcd
schedule
29.01.2014