【问题标题】:Android Studio turning black colors to white when switched to dark mode切换到深色模式时,Android Studio 将黑色变为白色
【发布时间】:2021-03-27 09:32:16
【问题描述】:

我是 android studio 的新手,我正在设计一个必须从 android 存储设备打开 pdf 文件的应用程序。我在活动视图中将背景颜色设置为黑色,只要我不在黑暗模式下使用我的 android,它就可以正常工作。一旦我打开深色模式,背景颜色就会从黑色变为白色。

活动视图代码:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPdfActivity"
android:background="@color/black"
android:orientation="vertical"
>

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@color/black"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

主题.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.PDFwordmeaning" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/black</item>
    <item name="colorSecondaryVariant">@color/black</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="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

夜晚\themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.PDFwordmeaning" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/black</item>
    <item name="colorSecondaryVariant">@color/black</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>

colors.xml

<?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 xml android-layout kotlin


    【解决方案1】:

    您需要检查theme/night,因为它会显示将在暗模式下使用的颜色。 theme 只会显示正常使用时使用的颜色。

    【讨论】:

    • 我在 night\themes.xml 中将颜色 Primary 和 colorPrimaryDark 设置为黑色,但我仍然面临同样的问题。
    • 你可以分享代码吗? colors.xml、themes.xml 和 night/themes.xml ?
    • 我编辑了问题,现在您可以看到您要求的文件的代码。
    • 我在我朋友的手机[One Plus] 上安装了该应用程序,它在黑暗模式下运行良好,显然我只在我的手机上获得了白色背景 [redmi note 7 pro]。其他 pdf 查看应用程序(如 Google drive)在我的设备上运行良好,我想知道我的应用程序出了什么问题
    • 好吧,虽然我还没有听说过这样的问题 xD,也许其他人可以帮助你解决它......
    【解决方案2】:

    我遇到了类似的问题。

    https://support.google.com/accessibility/android/answer/6151800?hl=en

    如上所述,如果可以解决问题,请尝试在手机上使用深色主题并在手机上打开颜色反转。那么您的应用程序不支持深色主题。所以有些设备会强制它导致这种行为。

    您可以通过简单地将以下内容添加到主题.xml 中的样式来解决此问题

     <item name="android:forceDarkAllowed">false</item>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 2020-05-07
      • 2017-07-13
      • 2013-09-17
      相关资源
      最近更新 更多