如何在主题中全局更改 android 按钮文本颜色

How to change android button text color globally in theme

如何更改所有按钮的文本颜色?

我知道我可以像下面这样设置背景颜色:

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="colorButtonNormal">@color/buttonColor</item>
</style>

如何为按钮文本执行此操作?

 <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="android:textColor">#yourcolor</item>
    <item name="android:buttonStyle">@style/ButtonColor</item>
    <item name="colorButtonNormal">@color/buttonColor</item>
</style>



<style name="ButtonColor" parent="@android:style/Widget.Button">
   <item name="android:textColor">@color/yourcolor</item>
</style> 

android:textColor 这应该可以帮助您全局更改文本颜色。

如果你只是需要改变按钮的文字颜色。您可以修改主主题的 textAppearanceButton 样式属性。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorButtonNormal">@color/buttonColor</item>
    <item name="android:textAppearanceButton">@style/TextAppearance.AppCompat.Button.Custom</item>
</style>

并声明您的新 textAppearance 样式如下

<style name="TextAppearance.AppCompat.Button.Custom">
    <item name="android:textColor">@color/mycustomcolor</item>
</style>

对于任何偶然发现此问题的人,我建议查看关于 Material Design Button Styles. 的类似问题。使用主题,您的 "accent color" 将用于为您的按钮着色。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="android:buttonStyle">@style/Widget.AppCompat.Button.Colored</item>
    <item name="buttonStyle">@style/Widget.AppCompat.Button.Colored</item>
</style>

只需设置此属性即可使用 AppCompatButton :

"app:backgroundTint="@color/wildberries"

并确保您的 activity 扩展了 AppCompatActivity。我只是在我的项目中使用它。它在 5.X 和 5.X.

之前都非常有效

在您的应用主题中使用新的 Theme.MaterialComponents theme you can define the materialButtonStyle 属性。

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ....

    <item name="materialButtonStyle">@style/MyButtonTheme</item>
  </style>

通过这种方式,您可以全局自定义应用中所有按钮的样式。 您可以使用 materialThemeOverlay 属性覆盖默认样式的主题属性。

类似于:

  <style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ButtonStyleTextColor</item>
  </style>

  <style name="ButtonStyleTextColor">
    <!-- For filled buttons, your theme's colorPrimary provides the default background color of the component, and -->
    <!--the text color is colorOnPrimary -->
    <item name="colorPrimary">@color/my_color</item>
    <item name="colorOnPrimary">@color/my_color2</item>

  </style>

目前 materialThemeOverlay 属性需要 android 库的 material 组件版本 1.1.0

<Button  
     app:backgroundTint="#fece2f" //any color
 />

如果您不想全局定义它,这是最好的方法