Android 将 textColor 值设置为全局

标签 android styles textcolor

我正在做一些教程,我尝试尝试使用 textColor。使用每个 Widget 中的属性、布局 xml 或样式属性,这可以很好地工作。但我想知道是否可以定义一个全局 textColor,据我所知是不可能的。

我在这些页面上找到了解决方案:

http://www.therealjoshua.com/2012/01/styling-android-with-defaults/

http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

在我的小程序中,我使用了 EditText、RadioButton 和 Button。如果我使用带有父 @android:TextAppearance 的样式 block 设置每个单个小部件的 TextAppearance,则小部件将丢失其其他属性。所以我搜索了 style.xml 和 theme.xml 并找到了一个非常简短的解决方案:

<resources>
  <style name="MyTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:editTextColor">@android:color/white</item>
    <item name="android:textColorPrimaryDisableOnly">#FFFFFF</item>
    <item name="android:color/primary_text_light">#FFFFFF</item>      
  </style>           
</resources>

尽管在 theme.xml 中使用了预定义的颜色

<style name="TextAppearance.Widget.Button" parent="TextAppearance.Small.Inverse">
    <item name="android:textColor">@android:color/primary_text_light_nodisable</item>
</style>

我的第一个问题:有没有办法像我对其他属性所做的那样覆盖@android:color/primary_text_light_nodisable 的值?

其实我觉得我自己解决了这个问题:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="MyTheme" parent="android:Theme.Light">
    <item name="android:textAppearance">@style/MyTextAppearance</item>
    <item name="android:textAppearanceButton">@style/MyTextAppearanceButton</item>
    <item name="android:editTextStyle">@style/MyEditTextStyle</item>
    <item name="android:radioButtonStyle">@style/MyRadioButtonStyle</item>     
  </style>

  <style name="MyTextAppearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@android:color/white</item>
  </style>

  <style name="MyTextAppearanceButton" parent="@android:style/TextAppearance.Widget.Button">
    <item name="android:textColor">@android:color/white</item>
  </style>

  <style name="MyEditTextStyle" parent="@android:style/Widget.EditText">
    <item name="android:textColor">@android:color/white</item>
  </style>

  <style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:textColor">@android:color/white</item>
  </style>

</resources>

所以我的第二个问题是,这是否是正确的方法,或者是否有一种简短的方法来全局更改文本的颜色?

虽然我认为在 xml 中定义所有这些是最干净的方法,但我想知道您是否建议通过类方法来操作它?

抱歉,我是 Java 和 Android 编程的新手,感谢您的帮助!

最佳答案

您可以在 res/values 下创建一个 colors.xml 并从这里声明您的全局颜色。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="scheme">#007BB3</color>

    <color name="blue">#0000FF</color>
    <color name="white">#FFFFFF</color>
    <color name="grey">#C1C1C1</color>  
</resources>

关于Android 将 textColor 值设置为全局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12057207/

相关文章:

java - 如何在运行时显示多个图像(换行到下一行)

android - 编写一个可以跨设备更新的应用程序

android - 从 android studio 的图库中选择一张图片?

button - Xamarin Forms DisplayAlert 按钮 TextColor

css - 使用 VML,是否可以设置文本颜色和位置?

java - 如何在 FirebaseRecyclerAdapter 中设置 orderbychild?

android - ListView 点击事件

python - 使用 Pandas 风格我得到 KeyError : "None of . ...位于 [列]

android - 如何将样式应用于 Android 应用程序中的所有按钮?

android - 在 CordinatorLayout 中的 ImageView 底部添加阴影