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

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

但是还是不行。

这里是 another post关于同样的问题。

最佳答案

在您的 Manifest 中,您需要引用其中包含文本颜色 itemstyle 的名称。现在你只是在引用一个空的 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>

list :

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

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

关于android - 如何使用自定义主题更改默认文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593133/

相关文章:

c - 如何将 48 位 RRRRGGGGBBBB 颜色转换为 24 位 RRGGBB 颜色格式

android - 启动画面仅在 Android 8.1 上触发 Resources$NotFoundException

java - Android FileObserver onEvent 没有被调用

java - java中的多彩多变的文本

android - 如何让我的自定义帐户类型显示在 Android 通讯录应用程序中?

html - 更改 html 中的文本颜色不起作用

android-如何使按钮背景不透明

android - 扩展外部应用程序样式/主题

android - Qt Quick Controls 2 启动画面

Android KTX 或 Anko