如何使用自定义主题更改默认文本颜色? [英] How to change default text color using custom theme?
问题描述
我正在尝试的主题应该很容易,但我不知道如何:我希望我的应用程序中的所有文本默认为白色.我在 theme.xml 中创建了一个自定义主题:
What I'm trying should be quite easy with themes, but I can't find out how to: I want all text to be white by default in my app. I created a custom theme in theme.xml:
<style name="Theme" parent="@android:Theme">
</style>
<style name="TextAppearance.Theme" parent="@android:TextAppearance.Theme">
<item name="android:textColor">#ffffffff</item>
</style>
并为整个应用程序设置:
and set it for the whole application:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/Theme">
但标签仍然是黑色的.缺少什么?
But labels are still black. What's missing?
PS:我如何为不同的文本大小另外定义样式,以应用于每个小部件?这样的说法正确吗?
PS: How can I additionally define styles for different text sizes, to be applied per widget? Is something like that correct?
<style name="Theme.smallText">
<item name="android:textSize">12dp</item>
</style>
更新
我查看了 Android SDK 中的 themes.xml,它展示了如何为主题设置文本样式:
I took a look at themes.xml in Android SDK, it shows how to set the text style for a theme:
<item name="textAppearance">@android:style/TextAppearance</item>
就我而言,它应该适用于这个定义:
In my case it should work with this definition:
<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>
但是,它仍然无法正常工作.
However, it is still not working.
这是关于同一问题的另一篇博文.>
Here's another post about this same issue.
推荐答案
在您的 Manifest
中,您需要引用具有文本颜色 的
在里面.现在你只是引用一个空的style
的名称>itemstyle
.所以在你的 theme.xml 中只做这个 style
:
In your Manifest
you need to reference the name of the style
that has the text color item
inside it. Right now you are just referencing an empty style
. So in your theme.xml do only this style
:
<style name="Theme" parent="@android:style/TextAppearance">
<item name="android:textColor">#ffffffff</item>
</style>
并让您在 Manifest
中引用相同的 (android:theme="@style/Theme"
)
And keep you reference to in the Manifest
the same (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
中引用它.我将 parent
值更改为 @android:style/TextAppearance
.
Notice I combine the text color and size into the same style
. Also, I changed the name of the theme to MyTheme and am now referencing that in the Manifest
. And I changed to @android:style/TextAppearance
for the parent
value.
这篇关于如何使用自定义主题更改默认文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!