【问题标题】:Error setting color for Text in TextView in Android Studio在 Android Studio 中为 TextView 中的文本设置颜色时出错
【发布时间】:2021-03-01 16:44:21
【问题描述】:

我正在尝试在 Android Studio 中设置 TextView 的文本颜色,并且在 Android Studio 的预览中它看起来很好......但是,在物理设备上运行它时,它会显示完全不同的颜色。

这是 XML 文件中的 TextView

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="268dp"
    android:text="Book S Mart is a place where \npeople can exchange used and new\nbooks of all types.\n\nEvery year millions of books\nare sent to recycling although they can be used again\n\nOur goal is to provide these books to those in need as well as Help the environment"
    android:textAlignment="center"
    android:textColor="@color/homeText"
    android:textSize="22sp"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

这是colors.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="homeText">#060E5A</color>
</resources>

它在 Android Studio 中的显示方式如下:

这是它在我的设备上的显示方式(使用 USB 调试)

如何正确设置颜色?

【问题讨论】:

  • 非常奇怪的行为...也许尝试在 Android Studio 中构建 -> 清理项目,然后安装调试版本
  • 你能确认JAVA文件没有任何与Textcolor相关的代码吗?
  • Java 文件中没有与 TextColor 相关的代码...
  • 在不同的物理设备上试试看

标签: java android text colors formatting


【解决方案1】:

这取决于设备,请尝试使用其他设备或模拟器。这不是颜色代码问题。

【讨论】:

    【解决方案2】:

    可能的解决方案:

    1. 不使用colors.xml 尝试不同的颜色。 (例如:android:textColor="#000000"

    2. 在模拟器上试试。

    3. 转到File &gt; Invalidate Caches / Restart。它将清除缓存并重新启动 Android Studio。

    4. 转到Build &gt; Clean Project,然后转到Build &gt; Rebuild Project

    您可以尝试这些解决方案。

    【讨论】:

    • 我在模拟器上运行它,它运行良好,但是在我将它捆绑并作为 APK 共享后,如何确保在任何其他设备上都不会出现此错误?
    • 这个问题出现在所有颜色还是只有这个颜色?
    • 我尝试了各种深浅不一的蓝色和黑色......问题出现在所有这些
    • 可能是安卓版本问题。你用的是哪个版本?
    • 我正在为 22 及更高版本编译,但在 30 上运行它
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多