【问题标题】:AppCompat v22: Set default textcolor for TextViewAppCompat v22:为 TextView 设置默认文本颜色
【发布时间】:2015-07-15 08:38:31
【问题描述】:

我想在我的 AppTheme 中设置一个默认的文本颜色,它应该是黑色(不是默认的 Material Design 深灰色)。应该通过在 UI 元素(例如 TextView)上通过 android:textAppearance-attribute 设置自定义样式来覆盖 textColor。

这是我目前的设置:

我在我的项目中使用 AppCompat-Library:

compile 'com.android.support:appcompat-v7:22.2.0'

我定义了以下主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorPrimary">@color/primary</item>
  <item name="colorPrimaryDark">@color/primary_dark</item>
  <item name="colorAccent">@color/accent</item>
  <item name="android:textColorPrimary">@color/black</item> <!-- All TextViews should have this color as default text color -->
</style>

在AndroidManifest.xml中设置:

<application
  android:name=".core.BaseApplication"
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme">

另外我定义了一些样式来改变一些TextViews的textAppearance:

<style name="App.Heading" parent="android:TextAppearance.Widget.TextView">
  <item name="android:textSize">16sp</item>
  <item name="android:textColor">@color/red</item>
</style>

<style name="App.Heading.Highlighted" parent="android:TextAppearance.Widget.TextView">
  <item name="android:textSize">16sp</item>
  <item name="android:textColor">@color/blue</item>
</style>

现在我有了一个带有一些 TextView 的 xml 布局。其中一些应该具有默认的 textAppearance(实际上是在我的主题中定义为 android:textColorPrimary 的黑色文本颜色)。有些人应该从我定义的样式中应用自定义文本外观:

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginTop="10dp"
     android:text="CUSTOM: App.Heading"
     android:textAppearance="@style/App.Heading" />

  <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginTop="10dp"
     android:text="CUSTOM: App.Heading.Highlighted"
     android:textAppearance="@style/App.Heading.Highlighted" />

  <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginTop="10dp"
     android:text="Normal Textview with no style"/>

前两个TextView应用我定义的textAppearance,这很好。但最后一个 TextView 有一个深灰色的文本颜色(材料设计默认?)而不是黑色的。如果我设置属性:

<item name="android:textColor">@color/black</item>

在我的 AppTheme 中,所有的 TextView 都有黑色的文本颜色。我的样式中定义的文本颜色(例如 App.Heading)不再被识别。

所以我的问题是:如何为我的 TextView 设置默认的 textColor,可以通过设置 textAppearance 来覆盖它?

【问题讨论】:

  • 克里斯托弗;你有解决办法吗?
  • @VahidGhadiri 抱歉,没有直接的解决方案。我使用了下面提到的解决方法。
  • 如果您将主题AppTheme 设置为这些TextViews ViewGroup,它是否有效。也许它只是忘记了是什么。让我知道
  • 使用 android:textColorTertiary 选项。我遇到过类似的问题,textColorTertiary 解决了问题

标签: android textview android-appcompat


【解决方案1】:

很难找到方法,因为有三个可样式化的android:textColor 属性,textColorPrimary textColorSecondary 和 textColorTertiary,还有很多基本样式属性android:textAppearance as small medium 等(现在已弃用?)。

无论如何,关于没有样式的 TextView,它似乎默认引用了 android:textAppearanceSmall 样式属性,其值是样式 TextAppearance.AppCompat.Small,它通过 Base.TextAppearance.AppCompat.Small 覆盖 android:textColor 属性和 ?android:attr/textColorTertiary 值。

然后像下面这样覆盖它会起作用:

<item name="android:textColorTertiary">@color/black</item>

【讨论】:

  • 谢谢!您在 Android 的代码中哪里发现 TextView 使用 textAppearanceSmall 而不仅仅是 textAppearance ?
  • 我真的不记得了,我想我在 res /values 中阅读了样式 AppCompat 并进行了排除
  • 在平台的 Widget.TextView 样式中找到(不是在 appcompat 中)。谢谢!
  • 我的是经验方法,你的理论更好。谢谢你。
  • 你应该把&lt;item name="android:textColorTertiary"&gt;@color/black&lt;/item&gt;这一行放在AppTheme的什么地方?
【解决方案2】:

如果我没记错的话,你不能使用 textAppearance 设置文本的颜色。您需要使用样式或主题来实现此目的。

【讨论】:

  • 嗯,文档说的有些不同。 developer.android.com/reference/android/widget/…:基本文本颜色、字体、大小和样式。 -> 还是我误解了什么?
  • 文档说那是基础,但是在基础之后是样式,然后是主题。创建一个定义 textColor 和 textAppearance 的样式并将该样式分配给 TextView 怎么样?
  • 好的,谢谢。我不知道。如果我将自定义 textAppearances 设置为样式,它就可以工作。定义样式似乎也是一种解决方法,我想避免。
【解决方案3】:

请在 v11 、v14 和 v21 文件夹中的 style.xml 文件中定义以下代码。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:textColor">@android:color/red</item>
</style>

并为每个文本视图使用此样式。

【讨论】:

  • 谢谢,但我想避免这种情况,并想使用默认值。从我的角度来看,这只是一种解决方法。
  • 那么有没有办法定义一个全局默认值呢?我认为我的主题中的属性 textColorPrimary 应该可以工作,但它没有。
  • 您已经在 v14、v11 和 v21 文件夹中的所有 style.xml 文件中定义了颜色
  • 我已经这样做了。请参阅我的问题的最后一部分。在这种情况下,我的自定义样式中的 textcolor 在将它们用作 textAppearance 时不再被识别。我可以使用样式属性,但因此我更喜欢使用自定义样式的解决方案作为默认行为。
猜你喜欢
  • 2012-09-13
  • 2017-07-28
  • 2022-11-13
  • 2011-09-22
  • 1970-01-01
  • 2021-05-24
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多