安卓 - 暗黑模式问题:黑色文本在暗色背景上

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

enter image description here

我从未更改过 Android Studio 的默认颜色设置,因此我拥有默认的两个主题 XML 和 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物理设备进行测试。感谢您的帮助。


问题只出现在安卓设备上吗?如果是的话,并且您使用Chrome浏览器 - 请检查chrome://flags,以确保您没有错误地打开了“强制暗模式”标志。 - Spoderman4
7个回答

25

前往activity_main.xml,并将颜色保持为黑色的TextView的textColor属性设置为

android:textColor = "?android:textColorPrimary"

进入 res/values/themes/themes.xml (night),并在 <!-- Customize your theme here. --> 下方添加以下代码:

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

前往res/values/themes/themes.xml并在<!-- 自定义您的主题 -->下方添加以下代码:

前往res/values/themes/themes.xml並在<!-- 自訂您的主題 -->下方添加以下程式碼:

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

运行您的应用程序并切换到暗模式。它会起作用,只需确保在themes.xmlthemes.xml(night)文件中的</style>闭合标签之前添加代码。


看起来像巫术,但它确实有效! - portsample

5

要在夜间将颜色变为白色和浅色变为黑色,请使用以下代码。

• 打开 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>

• 最后一步,在活动或片段的 TextView 中应用此行。
  android:textColor = "?android:textColorPrimary"

完成 坚持不懈的代码 ☻♥


4

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

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

3

将这行中的颜色改为其他颜色即可解决问题。

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

很遗憾,它不起作用。我已经尝试过使用“white”,但似乎它并没有影响我的菜单。 - Andry
你的应用程序默认背景是黑色吗? - Ayush Pal
尝试使用colorOnSurface。 - Ayush Pal
不,白色是默认的背景颜色。我没有更改任何东西。当在Android Studio中新建项目并选择“导航抽屉活动”模板时,这些是默认颜色。编辑:在night\theme.xml中添加<item name="colorOnSurface">@color/white</item>并没有改变任何东西。 - Andry
2
<item name="android:textColor">@android:color/holo_red_light</item> 这对我有效。 - Ayush Pal
这个有效!它将文本颜色从黑色改变为红色(现在我改成了白色)。谢谢。唯一仍有问题的是使用 https://github.com/ISchwarz23/SortableTableView 制作的表格,在深色模式下仍然存在相同的问题(黑色文本,深色背景),但我认为我可以使用提供的文档和 if 循环手动编辑颜色以获取深色模式。 - Andry

2

只需要添加@color/white,确保在清单文件中声明了您的主题名称。

<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>

1

如何设置文本颜色,因为没有为文本设置颜色?

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


我从未设置过它,实际上,我正在搜索文本颜色,但没有可用的字符串。已经尝试将colorOnPrimary和colorOnSecondary都更改为白色,但这并没有解决问题。 - Andry

0

夜间模式激活时更改TextView的文本颜色

 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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接