【问题标题】:Dark theme not applied correctly, statusbar color unchanged未正确应用深色主题,状态栏颜色未更改
【发布时间】:2022-01-06 17:41:42
【问题描述】:

我正在尝试为我的应用程序创建 lightdark 主题。 应用浅色主题时,状态栏应为橙色,但当我切换到深色主题时,状态栏保持橙色,尽管我希望它是黑色。

我不是主题方面的专业人士,因此非常感谢任何帮助。

我附上了一些截图,所以你可以明白我的意思。

提前谢谢你。

编辑:

我自己找到了解决方案(如果其他人也有同样的问题),在我的 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


【解决方案1】:

去你的风格

将此代码添加到您的主题风格中

    <item name="colorPrimary">#000000</item>
    <item name="colorPrimaryDark">#000000</item>

【讨论】:

  • 嗨,这也将工具栏更改为黑色,当应用浅色主题时它应该保持橙色。在应用“深色主题”时,我只希望“状态栏”也为黑色,工具栏在浅色和深色主题中应保持橙色。请看看我的 style.xml
  • oo 这种情况下转到您的布局 xml 文件并添加此代码 android:background="#000000"
  • 我更新了深色主题的屏幕截图,所以你知道我的意思。如果您能提供帮助,那就太好了,也许它s also something in the AndroidManifest.xml what I posted? Dont 知道。
  • 看我给了你另一个答案..你所要做的就是打开你的活动 xml 文件并在顶部布局上添加这个 1line 代码
  • 你明白了吗?或者把你的 mainactvity xml 文件发给我,我会告诉你的。
【解决方案2】:

在您的父布局中 添加此代码

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#000000"//this is the main code use this line in your code
    ></LinearLayout>

【讨论】:

  • 嗨,谢谢,但这不是它应该如何工作的。应该是,当我应用浅色主题时,状态栏应该是橙色,wgen 我应用深色主题时,状态栏应该是黑色。
【解决方案3】:

在 values-v21 的 styles.xml 中正确设置 colorPrimaryDark,如下所示 -

<style name="AppTheme" parent="Theme.AppCompat.YourTheme">

        <item name="android:colorPrimary">@color/PrimaryColor</item>
        <item name="android:colorPrimaryDark">@color/PrimaryBackgroundDark</item>

    </style>

要更改状态栏颜色,您必须更改 - colorPrimaryDark

对于应用栏,您必须更改 - colorPrimary

【讨论】:

    【解决方案4】:

    只需编辑这个 &lt;item name="colorPrimaryDark"&gt;#000000&lt;/item&gt;

    Theme.AppCompat 默认使“colorPrimaryDark”成为您的状态栏颜色。 只需将其更改为任何值即可。

    “colorPrimary”值改变你的工具栏颜色

    希望能解决你的问题

    【讨论】:

      【解决方案5】:

      此代码将根据需要更改主题。您应该在您的 styles.xml 文件中同时覆盖和编辑 colorprimarycolorprimaryDark

      更新

      问题在于您在 LightTheme 中的父主题,父主题应该是 Theme.AppCompat.Light.DarkActionBar

      LightTheme:colorprimary=orange,colorprimaryDark=DarkOrange。

      DarkTheme:colorprimary=orange,colorprimaryDark=Black。

      查看以下代码:

      <!-- Light application theme. -->
      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <!--   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>
      <style>
      

      【讨论】:

      • 您好,是的,但是浅色主题中的 #1B1C1C 会将状态栏更改为深色,应用浅色主题时它应该保持橙色。应用深色主题时,状态栏应该变黑。请参阅我的第二个屏幕截图。我的清单中有什么东西吗?无论我尝试什么,在应用深色主题时状态栏都保持橙色。
      • 不幸的是,当我应用深色主题时,状态栏保持橙色并且不会变黑。我在我的 Android Manifest 中应用了浅色主题: 这和它没有关系吗?我有这种感觉。
      • 好的,很简单,用户应用深色主题,状态栏也应该变黑但保持橙色(见我的第二张截图)。用户应用浅色主题,状态栏应变为橙色(参见我的第一个屏幕截图)。仅此而已。这两个主题都在我的 styles.xml 文件中设置,但深色主题不起作用。 (状态栏保持橙色,但应变为黑色),#1B1C1C
      【解决方案6】:

      这适用于那些在明暗模式下努力改变状态栏颜色的人。 首先,检查设备是否处于暗模式选项。 之后应用此方法更改状态栏的颜色。它在我的设备上运行良好。

      fun checkDarkMode(context: Context): Boolean {
      
          val nightModeFlags: Int =
              context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
          when (nightModeFlags) {
              Configuration.UI_MODE_NIGHT_YES -> {
                  return true
              }
          }
          return false
      }
      
      
      
      
      fun setStatusBarColor(activity: Activity){
         var isInDarkMode= checkDarkMode(activity)
          val window = activity.window
          if (isInDarkMode) {
              window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
              window.statusBarColor = ContextCompat.getColor(activity, R.color.black)
          } else {
              window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
              window.statusBarColor = ContextCompat.getColor(activity, R.color.white)
              window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-11
        • 2020-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-19
        相关资源
        最近更新 更多