为什么TextView文本颜色会自动调整为背景色调

3
我最近开始学习Android开发。我使用了一个非常简单的XML布局,只包含一个LinearLayout和一些ImageViews以及TextViews。在我的styles.xml中使用的主题是:

Theme.AppCompat.Light.NoActionBar

对于TextViews,我还没有设置textColor,但是在我的手机上,文本颜色会自动调整并匹配相应的背景颜色(只有更多对比度时才会变暗)。我喜欢这种效果,但是我不知道它来自哪里。

这是其中一个TextView:

<?xml version="1.0" encoding="utf-8"?>        
<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:layout_gravity="center_vertical"
    android:textSize="22sp"
    android:textStyle="bold"
    android:text="Enjoy your view!" />

这是结果(自动文本颜色在红色选择中突出显示):

输入图像描述

哪个属性/设置/类或其他内置功能负责此操作?它是否取决于手机?还是主题?或者......?


1
Theme.AppCompat.Light 主题的默认文本颜色略带半透明,这就是为什么它看起来会根据不同的背景颜色进行调整的原因。(如果这是你的意思的话。) - Mike M.
1
哦,我的天啊...真的吗..现在我有点尴尬了。是的,这就是我想知道的。我只是不知道该去哪里寻找原因——颜色看起来调整得非常完美。:-D 所以它是黑白相间的,带有透明度。嗯,也许你想写一个答案,这样我就可以接受它作为正确的答案?也许有一天会有其他人和我一样困惑... - ho.s
3个回答

1
Theme.AppCompat.Light主题的默认文本颜色略带半透明,因此它看起来像是针对每个不同的背景颜色进行调整。
我们可以查看源代码来确定实际值,从您选择的主题开始,该主题在appcompat的themes.xml中定义:
<style name="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

这个在同样的XML中继承自Theme.AppCompat.Light

<style name="Theme.AppCompat.Light" parent="Base.Theme.AppCompat.Light" />

Base.Theme.AppCompat.Light 位于themes_base.xml中:

<style name="Base.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light">

这让我们想到了同一文件中的Base.V7.Theme.AppCompat.Light
<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">

然后到 Platform.AppCompat.Light,最终我们到达了一个颜色设置:

<style name="Platform.AppCompat.Light" parent="android:Theme.Light">
    ...
    <!-- Text colors -->
    <item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>
    ...

abc_primary_text_material_light实际上是一个ColorStateList,它在XML中使用<selector>元素定义,在appcompat的res/color/目录下有其文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/primary_text_disabled_material_light"/>
    <item android:color="@color/primary_text_default_material_light"/>
</selector>

最后,我们找到了默认颜色 - primary_text_default_material_light - 这是在res/values/colors_material.xml中定义的一个简单的颜色值:
<!-- 87% black -->
<color name="primary_text_default_material_light">#de000000</color>

我们可以看到颜色的alpha通道并不完全不透明,这解释了观察到的外观。

0
默认情况下,TextView 的文本颜色由您应用的主题决定,例如您的 Theme.AppCompat.Light.NoActionBar,但您可以通过在 TextView 中添加 android:textColor="#FF0000" 来更改它,这将更改您的文本颜色。以下是一个示例。
<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="TextView"
        android:textSize="36sp"
        android:textColor="#FF0000" />

0

主题驱动您的颜色和行为。 Android将根据您的基本应用程序主题,对文本应用默认颜色。

您不应该让机会来选择您的颜色、字体大小和其他类似的事情。使用样式来创建您的通用需求、大小、字体和颜色,并在整个应用中应用该样式。

例如:

 <!--Define custom font type-->
<style name="TextAppearance.Light" parent="android:TextAppearance">
    <item name="fontPath">fonts/Roboto-Light.ttf</item>
</style>

<style name="TextAppearance.Medium" parent="android:TextAppearance">
    <item name="fontPath">fonts/Roboto-Medium.ttf</item>
</style>

<style name="TextAppearance.Regular" parent="android:TextAppearance">
    <item name="fontPath">fonts/Roboto-Regular.ttf</item>
</style>

<!--TextView font type "Roboto-Light" style-->
<style name="MyAppTheme.TextView.Light">
    <item name="android:textAppearance">@style/TextAppearance.Light</item>
</style>

<style name="MyAppTheme.TextView.Light.Small">
    <item name="android:textSize">@dimen/text_size_small</item>
</style>

<style name="MyAppTheme.TextView.Light.Medium">
    <item name="android:textSize">@dimen/text_size_medium</item>
</style>

<style name="MyAppTheme.TextView.Light.Large">
    <item name="android:textSize">@dimen/text_size_large</item>
</style>

<style name="MyAppTheme.TextView.Light.14sp">
    <item name="android:textSize">@dimen/text_size_14</item>
</style>

   <style name="MyAppTheme.TextView.Light.12sp">
    <item name="android:textSize">@dimen/text_size_14</item>
</style>

<!--TextView font type "Roboto-Medium" style-->
<style name="MyAppTheme.TextView.Medium">
    <item name="android:textAppearance">@style/TextAppearance.Medium</item>
</style>

<style name="MyAppTheme.TextView.Medium.Small">
    <item name="android:textSize">@dimen/text_size_small</item>
</style>

<style name="MyAppTheme.TextView.Medium.Medium">
    <item name="android:textSize">@dimen/text_size_medium</item>
</style>

<style name="MyAppTheme.TextView.Medium.Large">
    <item name="android:textSize">@dimen/text_size_large</item>
</style>

<!--TextView font type "Roboto-Regular" style-->
<style name="MyAppTheme.TextView.Regular">
    <item name="android:textAppearance">@style/TextAppearance.Regular</item>
</style>

<style name="MyAppTheme.TextView.Regular.Small">
    <item name="android:textSize">@dimen/text_size_small</item>
</style>

<style name="MyAppTheme.TextView.Regular.Medium">
    <item name="android:textSize">@dimen/text_size_medium</item>
</style>

<style name="MyAppTheme.TextView.Regular.Large">
    <item name="android:textSize">@dimen/text_size_large</item>
</style>

<style name="MyAppTheme.TextView.Regular.13sp">
    <item name="android:textSize">@dimen/text_size_13</item>
</style>

//通用TextView主题

 <!--TextView text color and single line true-->
<style name="MyAppTheme.TextView">
    <item name="android:singleLine">true</item>
    <item name="android:textColor">@android:color/white</item>
</style>

//现在从这里开始创建与您正常文本主题不同的那些。 然后,除非您在布局文件中使用style="@styles/MyAppTheme.SomeOtherTextStyle"更改样式,否则所有textview都将遵守上面的样式。

例如:

<TextView
                android:id="@+id/txtLastSavedLabel"
                style="@style/MyAppTheme.TextView.Light.Small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="@string/configuration_list_item_last_saved" />

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