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

34

我该如何改变所有按钮文字的颜色?

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

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

我该如何对按钮文本执行此操作?


1
请查看此链接:https://dev59.com/xGzXa4cB1Zd3GeqPT3dk - M D
7个回答

51
 <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 这应该能帮助您全局更改文本颜色。


这只适用于按钮吗?还是适用于“editText”和“textView”? - Zapnologica
我已经编辑了代码。这个代码仅适用于按钮。旧的代码适用于所有情况。谢谢。我希望这对你有所帮助。 - Sumanth Jois
4
使用@style/Widget.AppCompat.Button作为ButtonColor的父样式,即可获得Material Design AppCompat样式。 - levibostian
1
在这个答案中,<item name="android:textColor">#yourcolor</item> 似乎不仅适用于按钮,而且适用于所有东西。 - zeus

16

使用新的Theme.MaterialComponents主题,您可以在应用程序主题中定义materialButtonStyle属性,全局自定义应用中所有按钮的样式。

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
       <!-- .... --> 
       <item name="materialButtonStyle">@style/Widget.App.Button</item>
  </style>

随着

<style name="Widget.App.Button" parent="Widget.Material3.Button">
    <!-- button style customization example -->
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
    <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
    <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
</style>

如果您正在使用Material2主题,请使用Widget.MaterialComponents.Button作为父级。

如果您只想覆盖默认样式中的某些属性(例如颜色),请使用materialThemeOverlay属性。

示例:

  <style name="Widget.App.Button" parent="Widget.Material3.Button">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
  </style>

  <style name="ThemeOverlay.App.Button">
    <!-- 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>

1
像魔法一样运作。 - JustGotStared

11

如果有人偶然看到这个问题,我建议查看一个关于Material Design按钮样式的类似问题。通过主题,您的“强调颜色”将用于对按钮进行着色。

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

11

如果您只需要更改按钮的文本颜色,可以修改主题的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>

1

由于MaterialComponents按钮默认使用colorPrimary,因此在使用Theme.MaterialComponents.Light时,您可以简单地在主题中设置colorOnPrimary,不知道在Material3中是否相同。

<resources>
    <!-- Base application theme. -->
    <style name="Base.Theme.MyApplication" parent="Theme.MaterialComponents.Light">
        <!-- Customize your light theme here. -->
        ...
        <item name="colorOnPrimary">@color/colorOnPrimary</item>
    </style>

    <style name="Theme.MyApplication" parent="Base.Theme.MyApplication" />
</resources>

-1

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

"app:backgroundTint="@color/wildberries"

确保你的活动继承自AppCompatActivity。我在我的项目中使用它。它在5.X和pre-5.X中都能完美运行。


这不会改变按钮文本的颜色。 - ban-geoengineering

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

如果您不想将其定义为全局变量,这是最佳方法


2
这不会改变按钮文本的颜色。 - ban-geoengineering

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接