如何更改textview默认颜色(Theme.AppCompat.Light.DarkActionBar)

时间:2017-12-07 20:31:09

标签: android

我试图理解Android的主题和样式系统,并且基本上尝试做最简单的事情 - 更改TextView文本的默认颜色。

根据TextView的源代码,默认样式为:

<item name="textViewStyle">@style/Widget.TextView</item>

看看这种风格,我发现了以下内容:

<style name="Widget.TextView">
    <item name="textAppearance">?attr/textAppearanceSmall</item>
    ....
</style>

似乎默认的textAppearance设置为textAppearanceSmall(来自主题)。

我查看引用的textAppearanceSmall的themes.xml并找到:

<item name="textAppearanceSmall">@style/TextAppearance.Small</item>

啊哈 - 它引用了这个TextAppearance.Small:

<style name="TextAppearance.Small">
    <item name="textSize">14sp</item>
    <item name="textColor">?textColorSecondary</item>
</style>

好的,我们到了某个地方 - 也许吧?默认情况下,TextView使用颜色&#34; textColorSecondary&#34;。

第一个直接问题:为什么它引用如此奇怪? (问号但不喜欢&#39;?attr / textColorSecondary&#39;)?

我回到themes.xml文件并发现:

<item name="textColorSecondary">@color/secondary_text_dark</item>

此时我非常确定我必须在我的自定义AppTheme中覆盖textColorSecondary,如下所示:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:textColorPrimary">#00CC00</item>
</style>

这当然行不通......问题是 - 为什么?

BTW:我正在查看的所有文件(theme.xml&amp; styles.xml)都位于我的Android SDK目录中:

C:\AndroidSDK\platforms\android-26\data\res\values

1 个答案:

答案 0 :(得分:2)

试试这个:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
     <item name="android:textColor">@color/white</item>
</style>
相关问题