【发布时间】:2022-01-06 17:41:42
【问题描述】:
我正在尝试为我的应用程序创建 light 和 dark 主题。 应用浅色主题时,状态栏应为橙色,但当我切换到深色主题时,状态栏保持橙色,尽管我希望它是黑色。
我不是主题方面的专业人士,因此非常感谢任何帮助。
我附上了一些截图,所以你可以明白我的意思。
提前谢谢你。
编辑:
我自己找到了解决方案(如果其他人也有同样的问题),在我的 Loginactivity 开始时,我通过 SharedPrefs 文件检查应用了哪个主题。
// which theme is set.
SharedPreferences settings = getSharedPreferences(Helper.PREF_NAME, MODE_PRIVATE);
Helper.newTheme = settings.getInt("themeCustom", 0);
如果设置了黑色主题,那我自己用WindowManager修改状态栏:
if (Helper.newTheme == Helper.THEME_DARK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#1B1C1C"));
}
this.setTheme(R.style.DarkTheme);
===
}
案件结案..
样式.xml:
<resources>
<!-- reference to CardView White/Dark styles -->
<attr name="cardStyle" format="reference" />
<attr name="txtBgStyle" format="reference" />
<!-- Light application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#FD8300</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#F59F00</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#FF4081</item>
<item name="android:windowDisablePreview">true</item>
<!-- v7.widget.CardView background color -->
<item name="cardStyle">@style/CardView.Light</item>
<item name="txtBgStyle">@style/CardView.Light</item>
</style>
<!-- Dark application theme. -->
<style name="DarkTheme" parent="Theme.AppCompat">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#FD8300</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#1B1C1C</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#FAFAFA</item>
<!-- v7.widget.CardView background color -->
<item name="cardStyle">@style/cardStyle</item>
<item name="txtBgStyle">@style/txtBgStyle</item>
</style>
<!-- v7.widget.CardView dark style -->
<style name="cardStyle">
<!-- Card background color -->
<item name="cardBackgroundColor">#282929</item>
</style>
<!-- Custom dark style for textviews, layouts, etc -->
<style name="txtBgStyle">
<item name="android:background">#282929</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
样式 v21:
<resources>>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp.example.com.myapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:noHistory="false"
android:label="@string/title_activity_login"/>
</application>
</manifest>
【问题讨论】:
-
您必须覆盖styles.xml 中的主题,并更改其中的工具栏颜色。
-
@NarendraBaratam 我不知道该怎么做。你能给我的代码举个例子吗?
标签: android themes android-theme