Android - 深色模式问题:深色背景上的黑色文本

问题描述 投票:0回答:8

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

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 进行测试。感谢您的帮助

android colors themes android-dark-theme android-darkmode
8个回答
27
投票

转到您的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 并在

<!-- Customize your theme here. -->
:

下面添加以下代码
<item name="android:textColorPrimary">@color/black</item>

运行您的应用程序并更改为深色模式。它会起作用的。只需确保代码添加在

themes.xml
themes.xml (night) 文件中的 </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>

• 最后一步在 Activity 或片段的 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>

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,因为它是唯一与文本颜色匹配的颜色


0
投票

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

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

0
投票

我也有同样的问题。 用户询问菜单,对我来说没有任何影响。 我最终在白天和夜晚的 color.xml 中添加了一种新颜色。 在夜间文件中,它是#ffffffff,在白天文件中,它是#00000000。 我编写了一个简短的递归扩展,它遍历菜单并设置 eash item.title 的颜色:

private fun Menu.setTextColor(color: Int) { val itemCount = 大小() 变量我 = 0 同时(我< itemCount) { val item:MenuItem = getItem(i) val spannedTitle = SpannableString(item.title) spannedTitle.setSpan( ForegroundColorSpan(color), 0, item.title!!.length, 0 ) item.title = spannedTitle if (item.hasSubMenu()) { item.subMenu!!.setTextColor(color) } i++ } }

我从 onPrepareOptionsMenu 调用它:

覆盖 fun onPrepareOptionsMenu(menu: Menu): Boolean { 值 mc = menu.setTextColor(getColor(R.color.menu_text_color)) 返回 super.onPrepareOptionsMenu(菜单) }

© www.soinside.com 2019 - 2024. All rights reserved.