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

did*_*_X8 56 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>
Run Code Online (Sandbox Code Playgroud)

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

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme">
Run Code Online (Sandbox Code Playgroud)

但标签仍然是黑色的.少了什么东西?

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

<style name="Theme.smallText">
    <item name="android:textSize">12dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

更新

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

<item name="textAppearance">@android:style/TextAppearance</item>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

但是,仍然没有工作.

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

koo*_*ng3 51

在你的Manifest,你需要引用的名称style具有文本颜色item里面.现在你只是引用一个空的style.所以在你的theme.xml中只做这个style:

<style name="Theme" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并让你参考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>
Run Code Online (Sandbox Code Playgroud)

表现:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/MyTheme">
Run Code Online (Sandbox Code Playgroud)

请注意,我将文本颜色和大小组合在一起style.此外,我将主题的名称更改为MyTheme,现在正在引用它Manifest.我换@android:style/TextAppearanceparent价值.

  • sry,这不起作用.我得到"错误膨胀类android.widget.TextView.原因:资源不是ColorStateList (3认同)

Gen*_*ene 41

创建应用程序时,将在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>
Run Code Online (Sandbox Code Playgroud)

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的本地stlyes(我上面给出的链接).

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

<application
android:theme="@style/Theme.AppBaseTheme">
Run Code Online (Sandbox Code Playgroud)


Jon*_*nik 18

您不能将其@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>
Run Code Online (Sandbox Code Playgroud)

表现:

<application
    ...
    android:theme="@style/MyAppTheme">
Run Code Online (Sandbox Code Playgroud)

更新

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

正如我通过提出这个问题所学到,更好的方法是使用drawable定义颜色:

<item name="android:textColor">@drawable/default_text_color</item>
Run Code Online (Sandbox Code Playgroud)

... 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>
Run Code Online (Sandbox Code Playgroud)

  • 我要回答我自己的问题。您可以对“RadioButton”、“CheckBox”、“Switch”使用“android:textColorPrimaryDisableOnly”。您可以对“EditText”使用“android:editTextColor”和“android:textColorHint”。相关问题:[如何更改 Android 应用程序中所有文本的默认颜色?](http://stackoverflow.com/a/13142473/842697) (2认同)