68

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

但是,它仍然无法正常工作。

这是关于同一问题的另一篇文章。

4

5 回答 5

68

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

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

并让您参考Manifest相同的 ( android:theme="@style/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/TextAppearanceparent价值。

于 2012-03-06T23:28:48.230 回答
51

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

样式.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 是我放在可绘制文件夹中的自定义图像。这是一个 300x300 的 png 图像。

#295055 是深蓝色。

我的代码更改了背景和文本颜色。对于 Button 文本,请查看 Google 的原生 stlyes(我在上面给你的链接)。

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

<application
android:theme="@style/Theme.AppBaseTheme">
于 2014-02-24T10:03:13.730 回答
24

您不能@android:style/TextAppearance用作整个应用程序主题的父级;这就是为什么koopaking3 的解决方案似乎很糟糕的原因。

要使用自定义主题更改应用程序中各处的默认文本颜色,请尝试这样的操作。至少适用于 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">

更新

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

正如我通过提出这个问题所了解到的,更好的方法是使用可绘制对象来定义颜色:

<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>
于 2014-01-22T09:09:26.830 回答
1

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

于 2019-04-18T15:38:58.127 回答
-2
    <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>

试试这个...

于 2015-08-24T12:03:28.073 回答