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

问题描述 投票:52回答:5

我正在尝试使用主题应该很容易,但我无法找到如何:我希望我的应用程序默认所有文本都是白色的。我在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

android colors android-theme
5个回答
51
投票

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

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

并让你参考Manifest相同(android:theme="@style/Theme"

编辑:

theme.xml:

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

请注意,我将文本颜色和大小合并到相同的style中。此外,我将主题的名称更改为MyTheme,现在我在Manifest中引用该名称。我改为@android:style/TextAppearance获取parent值。


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"是谷歌的原生色彩。以下是本机样式的参考: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的原生样式(我上面给出的链接)。

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

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

18
投票

你不能使用@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">

Update

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

当我学习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>

1
投票

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


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

试试这个......

© www.soinside.com 2019 - 2024. All rights reserved.