【问题标题】:Why is TextView textColor automatically adjusted to the background color tone为什么TextView textColor会自动调整为背景色调
【发布时间】:2018-07-28 15:12:11
【问题描述】:

我最近开始学习 Android 开发。制作了一个非常简单的 XML 布局,只有一个 LinearLayout 包含一些 ImageViewsTextViews。我在 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!" />

这是结果(自动 textColors 以红色突出显示):

哪个属性/设置/类或其他内置功能对此负责?这取决于手机吗?还是主题?还是...?

【问题讨论】:

  • Theme.AppCompat.Light 主题的默认文本颜色略微半透明,这就是为什么它看起来会针对每种不同的背景颜色进行调整。 (如果这就是你的意思。)
  • 哦,我的.. 真的.. 现在我有点尴尬。是的,这就是我想知道的。我只是不知道去哪里找原因——颜色似乎调整得如此完美。 :-D 所以它是具有透明度的黑色/白色,这就是原因。嗯,也许你想写信回答,这样我就可以接受它是正确的?也许有一天会有人和我一样困惑的人......

标签: android colors textview android-color


【解决方案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中定义了一个&lt;selector&gt;元素,它的文件在appcompat的res/color/ directory下:

<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 通道并非完全不透明,这解释了观察到的外观。

【讨论】:

    【解决方案2】:

    默认情况下,TextView 文本颜色由您应用的 Theme 确定,就像您的 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" />
    

    【讨论】:

      【解决方案3】:

      主题驱动着你的色彩和行为。 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
                      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" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-19
        • 2011-05-17
        • 1970-01-01
        • 1970-01-01
        • 2014-01-16
        • 2012-06-20
        • 2020-12-15
        相关资源
        最近更新 更多