AppCompat v22:为TextView设置默认文本颜色。

9

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

这是我的当前设置:

我在项目中使用了AppCompat库:

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">

此外,我定义了一些样式来改变某些TextView的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-layout。其中一些应该具有默认的textAppearance(实际上是黑色文本颜色,如我的主题中定义的android:textColorPrimary)。有些应该应用于我定义的样式中的自定义textappearance:

 <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具有深灰色的文本颜色(Material Design默认颜色?),而不是黑色的。如果我设置属性:

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

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

因此,我的问题是:如何为我的TextView设置默认文本颜色,可以通过设置textAppearance进行覆盖?


克里斯托弗,你有找到任何解决方案吗? - Vahid Ghadiri
@VahidGhadiri 抱歉,没有直接的解决方案。我使用了下面提到的解决方法。 - Christopher
如果将主题“AppTheme”设置为这些TextView的ViewGroup,它是否有效?也许只是失去了追踪什么是什么。请随时告诉我最新情况。 - Pouya Danesh
请使用android:textColorTertiary选项。我曾经遇到过类似的问题,而textColorTertiary解决了这个问题。 - ashraful
3个回答

16

由于存在三个可设置样式的android:textColor属性,即textColorPrimary、textColorSecondary和textColorTertiary,以及许多基础样式android:textAppearance,如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呢? - pdegand59
我真的不记得了,我想我在res/values中读取了AppCompat样式并进行了排除。 - GPack
在该平台的Widget.TextView样式中找到了它(而不是在appcompat中)。谢谢! - pdegand59
我的方法是经验性的,而你的理论性更好。谢谢你。 - GPack
这是确切的答案。如果我们使用 android:textColor,它将执行相同的操作,但在 pre lolipop 和 post lolipop 版本中也会影响 AlertDialog 积极和消极按钮的文本颜色。它将使用 android:textColor。 - ashraful
显示剩余2条评论

2

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


嗯,文档上说的有些不同。http://developer.android.com/reference/android/widget/TextView.html#attr_android:textAppearance: 基本文本颜色、字体、大小和样式。-> 或者我理解错了什么? - Christopher
1
文档中说这是基础,但在基础之上有样式,然后是主题。如果创建一个定义了textColor和textAppearance的样式,并将其分配给您的TextView,那怎么样? - Mimmo Grottoli
好的,谢谢。我不知道这一点。如果我将我的自定义textAppearances设置为样式,它就可以工作了。同时定义一个样式似乎是一种解决方法,但我想避免这样做。 - Christopher

1
请在v11、v14和v21文件夹中的style.xml文件中定义以下代码。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:textColor">@android:color/red</item>
</style>

并且对于每个TextView使用这种样式。

谢谢,但我想避免这种情况,我想使用默认设置。从我的角度来看,这只是一个解决方法。 - Christopher
那么没有办法定义全局默认值吗?我以为我的主题中的textColorPrimary属性应该起作用,但实际上并没有。 - Christopher
你需要在所有的style.xml文件中定义颜色,这些文件位于v14、v11和v21文件夹中。 - Anand Savjani
我已经做过了。请参见我的问题中的最后一部分。在这种情况下,当将它们用作 textAppearance 时,我的自定义样式中的文本颜色不再被识别。我可以使用 style 属性,但我更愿意使用默认行为的自定义样式解决方案。 - Christopher

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