【问题标题】:Android - Dark mode issue: black text on dark backgroundAndroid - 深色模式问题:深色背景上的黑色文本
【发布时间】:2020-11-23 15:46:41
【问题描述】:

当我启用暗模式时,我的应用程序上的某些菜单看起来很糟糕:非常暗背景上的黑色文本。我完全是色彩方面的初学者。

我还没有接触过android studio的默认颜色设置,所以我有默认的两个主题XMLs和Color Xml:

     <resources xmlns:tools="http://schemas.android.com/tools">
     <!-- Base application theme. -->
     <style name="Theme.TestSS" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
         <!-- Primary brand color. -->
         <item name="colorPrimary">@color/purple_500</item>
         <item name="colorPrimaryVariant">@color/purple_700</item>
         <item name="colorOnPrimary">@color/white</item>
         <!-- Secondary brand color. -->
         <item name="colorSecondary">@color/teal_200</item>
         <item name="colorSecondaryVariant">@color/teal_700</item>
         <item name="colorOnSecondary">@color/black</item>
         <!-- Status bar color. -->
         <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
         <!-- Customize your theme here. -->
     </style>

     <style name="Theme.TestSS.NoActionBar">
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
     </style>

     <style name="Theme.TestSS.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

     <style name="Theme.TestSS.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
   </resources>

还有:

    <resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
     <style name="Theme.TestSS" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
         <!-- Primary brand color. -->
         <item name="colorPrimary">@color/purple_200</item>
         <item name="colorPrimaryVariant">@color/purple_700</item>
         <item name="colorOnPrimary">@color/black</item>
         <!-- Secondary brand color. -->
         <item name="colorSecondary">@color/teal_200</item>
         <item name="colorSecondaryVariant">@color/teal_200</item>
         <item name="colorOnSecondary">@color/black</item>
         <!-- Status bar color. -->
         <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
         <!-- Customize your theme here. -->
     </style>
    </resources>

颜色:

   <?xml version="1.0" encoding="utf-8"?>
     <resources>
      <color name="purple_200">#FFBB86FC</color>
      <color name="purple_500">#FF6200EE</color>
      <color name="purple_700">#FF3700B3</color>
      <color name="teal_200">#FF03DAC5</color>
      <color name="teal_700">#FF018786</color>
      <color name="black">#FF000000</color>
      <color name="white">#FFFFFFFF</color>
     </resources>

问题出在哪里?我正在 Android R 上使用 AVD 进行测试,并在我的物理设备上使用 Android Q 进行测试。感谢您的帮助

【问题讨论】:

  • 这个问题只出现在android上吗?如果是,并且您使用 Chrome - 如果您没有错误地打开“强制暗模式”标志,请检查 chrome://flags。

标签: android colors themes android-dark-theme android-darkmode


【解决方案1】:

转到您的 activity_main.xml 并将颜色保持为黑色的 TextView 的 textColor 属性设置为

android:textColor = "?android:textColorPrimary"

转到 res/values/themes/themes.xml (night) 并在 &lt;!-- Customize your theme here. --&gt; 下方添加以下代码:

<item name="android:textColorPrimary">@color/white</item>

转到 res/values/themes/themes.xml 并在 &lt;!-- Customize your theme here. --&gt; 下方添加以下代码:

<item name="android:textColorPrimary">@color/black</item>

运行您的应用并更改为暗模式。它会工作的。只需确保在 themes.xmlthemes.xml (night) 文件中的 &lt;/style&gt; 结束标记之前添加代码

【讨论】:

    【解决方案2】:

    将此行中的颜色更改为其他颜色,这样就可以了

         <item name="android:textColor">@android:color/holo_red_light</item>
    

    【讨论】:

    • 遗憾的是它不起作用。我已经尝试过使用“白色”,但它似乎不会影响我的菜单
    • 黑色是你应用的默认背景吗??
    • 尝试使用 colorOnSurface
    • 不,默认背景是白色。我没有改变任何东西。这些是您在 Android Studio 的新项目中选择模板“导航抽屉活动”时的默认颜色编辑:在 night\theme.xml 上添加 @color/white 不会改变任何东西
    • @android:color/holo_red_light 这对我有用
    【解决方案3】:

    要更改菜单背景,请将以下行添加到您的样式中:

    <item name="android:itemBackground">@color/your_color</item>
    

    【讨论】:

      【解决方案4】:

      你是怎么设置文字颜色的 文字没有颜色设置?

      尝试更改此值@color/black,因为它是唯一与文本颜色匹配的颜色

      【讨论】:

      • 我从未设置过它,事实上,我正在寻找文本颜色,但没有可用的字符串。已经尝试在白色上同时更改 colorOnPrimary 和 colorOnSecondary 但这不能解决问题
      【解决方案5】:

      只需要添加这个@color/white,确保你已经在manifest中声明了你的主题名称。

      <style name="Theme.UserRestaurant" parent="Theme.MaterialComponents.Light.DarkActionBar">
          <!-- Primary brand color. -->
          <item name="colorPrimary">@color/orange</item>
          <item name="colorPrimaryVariant">@color/orange_dark</item>
          <item name="colorOnPrimary">@color/white</item>
          <item name="android:background">@color/white</item>
      
      
          <!-- Secondary brand color. -->
          <item name="colorSecondary">@color/teal_200</item>
          <item name="colorSecondaryVariant">@color/teal_700</item>
          <item name="colorOnSecondary">@color/black</item>
          <!-- Status bar color. -->
          <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
          <!-- Customize your theme here. -->
      </style>
      

      【讨论】:

        【解决方案6】:

        要在夜间将颜色更改为白色,将灯光更改为黑色,然后使用下面的代码。

        • 打开 res/drawable/values/themes.xml 并将此代码放入其中。

             <item name="android:textColorPrimary">@color/black</item>
        

        • 然后打开 res/drawable/values/themes.xml(night) 并将这段代码放入其中。

           <item name="android:textColorPrimary">@color/white</item>
        

        • 最后一步Apply in TextView of Activity or fragment 将这一行放入其中。

          android:textColor = "?android:textColorPrimary"
        

        完成保留代码☻♥

        【讨论】:

          【解决方案7】:

          启用夜间模式时更改文本视图的文本颜色

           public void checkNightModeActivated() {
                  if (preferences.getBoolean ( KEY_ISNIGHTMODE,false )) {
                      nightmode.setChecked ( true );
                      AppCompatDelegate.setDefaultNightMode ( AppCompatDelegate.MODE_NIGHT_YES );
          
          TextView textView=findViewById (R.id.textView );
          TextView.setcolor(color.white);
          

          【讨论】:

            猜你喜欢
            • 2017-06-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-06-20
            • 1970-01-01
            • 1970-01-01
            • 2012-04-19
            • 2020-11-28
            相关资源
            最近更新 更多