【问题标题】:How to change android button text color globally in theme如何在主题中全局更改android按钮文本颜色
【发布时间】:2016-07-15 15:47:35
【问题描述】:

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

我知道我可以如下设置背景颜色:

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

如何为按钮 Text 执行此操作?

【问题讨论】:

标签: android material-design android-button material-components material-components-android


【解决方案1】:
 <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 这应该可以帮助您全局更改文本颜色。

【讨论】:

  • 这仅适用于按钮吗?或editTexttextView
  • 我已经编辑了代码。此代码仅适用于按钮。年纪大的,适合所有人。谢谢。我希望这会有所帮助。
  • 有效,但使用@style/Widget.AppCompat.Button 作为 ButtonColor 的父样式以获取 Material Design AppCompat 样式。
  • 在这个答案中 #yourcolor 接缝不仅适用于按钮,也适用于一切
【解决方案2】:

使用新的Theme.MaterialComponents 主题,您可以在应用主题中定义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 属性需要1.1.0 版本的android 库材质组件。

【讨论】:

  • 像魔术一样工作。
【解决方案3】:
<Button  
     app:backgroundTint="#fece2f" //any color
 />

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

【讨论】:

  • 这不会改变按钮文本的颜色。
【解决方案4】:

只需通过设置此属性来使用AppCompatButton

"app:backgroundTint="@color/wildberries"

并确保您的活动扩展了 AppCompatActivity。我只是在我的项目中使用它。它在 5.X 和 pre-5.X 中都像一个魅力。

【讨论】:

  • 这不会改变按钮文本的颜色。
【解决方案5】:

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

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

【讨论】:

    【解决方案6】:

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

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多