【发布时间】:2014-11-20 15:46:43
【问题描述】:
我在 Play 商店中有一个应用程序:Just Notes
我最近为应用添加了主题。 问题是,状态栏颜色和默认背景颜色总是默认为清单中提到的主题。
所有其他主题都有其中提到的 colorPrimaryDark,但 Android 只是不使用它在运行时设置状态栏颜色。
如何正确覆盖清单中提到的主题
例如。 以下是我的风格
<style name="AppBaseThemeDark" parent="Theme.AppCompat"> <item name="colorPrimary">@color/primary_material_dark</item> <item name="colorPrimaryDark">@color/primary_dark_material_dark</item> <item name="colorAccent">@color/accent_material_dark</item> <item name="android:textColor">@color/abc_primary_text_material_dark</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style> <style name="AppBaseThemeLight" parent="Theme.AppCompat.Light"> <item name="colorPrimary">@color/primary_material_light</item> <item name="colorPrimaryDark">@color/primary_dark_material_light</item> <item name="colorAccent">@color/accent_material_light</item> <item name="android:textColor">@color/abc_primary_text_material_light</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style>
以下是我的清单
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppThemeDark" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
清单将应用主题设置为深色。
在我的活动中,这是我根据共享首选项中保存的内容设置主题的方式。
switch (mPrefs.getInt("theme", -1)) {
case 0:
setTheme(R.style.AppThemeDark);
break;
case 1:
setTheme(R.style.AppThemeLight);
break;
default:
//keep the theme from the manifest.
}
如果用户选择了浅色主题,状态栏和背景仍然保持黑色。
如果我将 manifest 中的主题更改为 AppBaseThemeLight,那么即使是深色主题,状态栏也会保持灰色。
【问题讨论】:
标签: android android-activity android-manifest android-theme