Прекарах много време, за да накарам това да работи и сега моето приложение има избираеми светли и тъмни теми, които дори могат да се избират динамично, влизайки незабавно в игра, включително 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