我有一个应用程序需要在用户的选择上应用不同的主题。所以,我创造了主题。通过 Manifest.xml 应用这些主题时工作正常。但是当我尝试以编程方式更改主题时,只有ActionBar颜色被更改,状态栏颜色保持不变,就像我在Manifest.xml中应用的主题一样。我的代码在下面。我已经尝试了所有可用资源但未能找到任何解决方案。
提前致谢。
的themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--<Cyan Theme Configuration>-->
<style name="Theme.Cyan" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary_color_cyan_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_cyan_variant</item>
<item name="colorAccent">@color/accent_color_for_cyan_variant</item>
<item name="android:icon">@drawable/ic_launcher</item>
</style>
<style name="Theme.Cyan.ToolBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary_color_cyan_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_cyan_variant</item>
<item name="colorAccent">@color/accent_color_for_cyan_variant</item>
</style>
<style name="Theme.Cyan.ToolBarStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">@color/primary_color_cyan_variant</item>
<item name="android:textColor">#FFF</item>
<item name="android:textColorSecondary">@color/primary_color_dark_cyan_variant</item>
</style>
<!--<Blue Theme Configuration>-->
<style name="Theme.Blue" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary_color_blue_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_blue_variant</item>
<item name="colorAccent">@color/accent_color_for_blue_variant</item>
<item name="android:icon">@drawable/ic_launcher</item>
</style>
<style name="Theme.Blue.ToolBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary_color_blue_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_blue_variant</item>
<item name="colorAccent">@color/accent_color_for_blue_variant</item>
</style>
<style name="Theme.Blue.ToolBarStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">@color/primary_color_blue_variant</item>
<item name="android:textColor">#FFF</item>
<item name="android:textColorSecondary">@color/primary_color_dark_blue_variant</item>
</style>
<!--<Teal Theme Configuration>-->
<style name="Theme.Teal" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary_color_teal_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_teal_variant</item>
<item name="colorAccent">@color/accent_color_for_teal_variant</item>
<item name="android:icon">@drawable/ic_launcher</item>
</style>
<style name="Theme.Teal.ToolBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary_color_teal_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_teal_variant</item>
<item name="colorAccent">@color/accent_color_for_teal_variant</item>
</style>
<style name="Theme.Teal.ToolBarStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">@color/primary_color_teal_variant</item>
<item name="android:textColor">#FFF</item>
<item name="android:textColorSecondary">@color/primary_color_dark_teal_variant</item>
</style>
<!--<Green Theme Configuration>-->
<style name="Theme.Green" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary_color_green_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_green_variant</item>
<item name="colorAccent">@color/accent_color_for_green_variant</item>
<item name="android:icon">@drawable/ic_launcher</item>
</style>
<style name="Theme.Green.ToolBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary_color_green_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_green_variant</item>
<item name="colorAccent">@color/accent_color_for_green_variant</item>
</style>
<style name="Theme.Green.ToolBarStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">@color/primary_color_green_variant</item>
<item name="android:textColor">#FFF</item>
<item name="android:textColorSecondary">@color/primary_color_dark_green_variant</item>
</style>
<!--<BlueGrey Theme Configuration>-->
<style name="Theme.BlueGrey" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary_color_blueGrey_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_blueGrey_variant</item>
<item name="colorAccent">@color/accent_color_for_blueGrey_variant</item>
<item name="android:icon">@drawable/ic_launcher</item>
</style>
<style name="Theme.BlueGrey.ToolBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary_color_blueGrey_variant</item>
<item name="colorPrimaryDark">@color/primary_color_dark_blueGrey_variant</item>
<item name="colorAccent">@color/accent_color_for_blueGrey_variant</item>
</style>
<style name="Theme.BlueGrey.ToolBarStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">@color/primary_color_blueGrey_variant</item>
<item name="android:textColor">#FFF</item>
<item name="android:textColorSecondary">@color/primary_color_dark_blueGrey_variant</item>
</style>
</resources>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.Green">
<!-- Customize your theme here. -->
</style>
<style name="Ocean" parent="Theme.Cyan"/>
<style name="Sky" parent="Theme.Blue"/>
<style name="Peacock" parent="Theme.Teal"/>
<style name="AppTheme.Grass" parent="Theme.Green"/>
<style name="Shadow" parent="Theme.BlueGrey"/>
<style name="menu_labels_style">
<!--<item name="android:background">@drawable/fab_label_background</item>-->
<item name="android:textColor">@color/white</item>
</style>
</resources>
的Manifest.xml
<application
android:name="com.compassites.employeedirectory.EmployeeDirectoryApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
MainActivity.java
super.onCreate(savedInstanceState);
setTheme(R.style.Ocean);
setContentView(R.layout.activity_main);
答案 0 :(得分:1)
在super.onCreate之前调用set主题 即。
setTheme(R.style.Ocean);
super.onCreate(savedInstanceState);