如何使用自定义主题更改默认文本颜色?

时间:2012-03-06 22:46:09

标签: android colors android-theme

我正在尝试的主题应该很容易,但我无法找到如何: 我希望我的应用程序默认所有文本都是白色的。 我在theme.xml中创建了一个自定义主题:

<style name="Theme" parent="@android:Theme">
</style>

<style name="TextAppearance.Theme" parent="@android:TextAppearance.Theme">
    <item name="android:textColor">#ffffffff</item>
</style>

并为整个应用程序设置它:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme">

但标签仍然是黑色的。 有什么遗漏?

PS:如何为每个小部件应用不同的文本大小另外定义样式? 是这样的吗?

<style name="Theme.smallText">
    <item name="android:textSize">12dp</item>
</style>

更新

我看了Android SDK中的 themes.xml ,它展示了如何设置主题的文字样式:

<item name="textAppearance">@android:style/TextAppearance</item>

在我的情况下,它应该适用于这个定义:

<style name="Theme" parent="@android:Theme">
    <item name="android:textAppearance">@style/MyText</item>
</style>

<style name="MyText" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
</style>

然而,仍然无法正常工作。

刚刚找到针对同一问题的其他帖子:http://groups.google.com/group/android-developers/browse_thread/thread/2897ccd0884546b9

5 个答案:

答案 0 :(得分:51)

Manifest中,您需要引用其中包含文字颜色style的{​​{1}}的名称。现在你只是引用一个空的item。所以在你的theme.xml中只做这个style

style

并让您在<style name="Theme" parent="@android:style/TextAppearance"> <item name="android:textColor">#ffffffff</item> </style> 相同的Manifest

中引用

修改

theme.xml:

android:theme="@style/Theme"

清单:

<style name="MyTheme" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
    <item name="android:textSize">12dp</item>
</style>

注意我将文本颜色和大小合并到同一个<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme"> 中。此外,我将主题的名称更改为MyTheme,现在我在style中引用该名称。我为Manifest值更改为@android:style/TextAppearance

答案 1 :(得分:40)

创建应用程序时,将在res / values文件夹中创建名为styles.xml的文件。如果更改样式,则可以更改所有布局的背景,文本颜色等。这样您就不必进入每个单独的布局并手动更改它。

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.AppBaseTheme" parent="@android:style/Theme.Light">
    <item name="android:editTextColor">#295055</item> 
    <item name="android:textColorPrimary">#295055</item>
    <item name="android:textColorSecondary">#295055</item>
    <item name="android:textColorTertiary">#295055</item>
    <item name="android:textColorPrimaryInverse">#295055</item>
    <item name="android:textColorSecondaryInverse">#295055</item>
    <item name="android:textColorTertiaryInverse">#295055</item>

     <item name="android:windowBackground">@drawable/custom_background</item>        
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

parent="@android:style/Theme.Light"是Google的原生颜色。以下是原生样式的参考: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

默认的Android风格也称为“主题”。所以你称之为主题可能会混淆程序。

name="Theme.AppBaseTheme"表示您正在创建一个继承parent="@android:style/Theme.Light"中所有样式的样式。 除非您想再次从AppBaseTheme继承,否则您可以忽略此部分。 = <style name="AppTheme" parent="AppBaseTheme">

@ drawable / custom_background是我放在drawable文件夹中的自定义图像。它是一个300x300 png的图像。

#295055是深蓝色。

我的代码会更改背景和文字颜色。对于Button文本,请查看Google的本地stlyes(我上面给出的链接)。

然后在Android Manifest中,记得包含代码:

<application
android:theme="@style/Theme.AppBaseTheme">

答案 2 :(得分:17)

您不能将@android:style/TextAppearance用作整个应用主题的父级;这就是koopaking3's solution看起来很破碎的原因。

要使用自定义主题更改应用中的默认文字颜色,请尝试以下操作。 至少适用于Android 4.0+(API级别14 +)。

res/values/themes.xml

<resources>    
    <style name="MyAppTheme" parent="android:Theme.Holo.Light">
        <!-- Change default text colour from dark grey to black -->
        <item name="android:textColor">@android:color/black</item>
    </style>
</resources>

清单:

<application
    ...
    android:theme="@style/MyAppTheme">

更新

上述缺点是禁用操作栏溢出菜单项使用默认颜色,而不是灰显。 (当然,如果您不在应用程序的任何位置使用禁用的菜单项,这可能无关紧要。)

在我了解by asking this question时,更好的方法是使用 drawable 定义颜色:

<item name="android:textColor">@drawable/default_text_color</item>

... res/drawable/default_text_color.xml指定单独的state_enabled="false"颜色:

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

答案 3 :(得分:1)

检查活动布局是否覆盖主题,并查找位于layout/*your_activity*.xml的活动布局,并查找包含android:textColor="(some hex code")内容的 TextView 类似于活动布局,然后将其删除。然后再次运行您的代码。

答案 4 :(得分:-2)

<color name="white">#FFFFFF</color>

<style name="Mytext" parent="@android:style/TextAppearance.Medium"> 
    <item name="android:textSize">20sp</item> 
    <item name="android:textColor">@color/white</item> 
    <item name="android:textStyle">bold</item>
    <item name="android:typeface">sans</item>
</style>

尝试这个...