How to change android button text color globally in theme

你离开我真会死。 提交于 2019-11-30 16:26:13

问题


How can I change all my buttons text color?

I know I can set the background color like follows :

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

How can I do this for the button Text?


回答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 This should help you change the text color globally. I hope this is helpful ThankYou




回答2:


If you just need to change the text colour of buttons. You can modify the textAppearanceButton style attribute of your main theme.

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

And declare your new textAppearance style as follows

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



回答3:


For anyone that stumbles onto this, I recommend checking out a similar question about Material Design Button Styles. With a theme, your "accent color" will be used to color your button.

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



回答4:


With the new Theme.MaterialComponents theme you can define the materialButtonStyle attribute in your app theme.

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

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

In this way you can customize globally the style of all buttons in your app. You can override the theme attributes from the default style using the materialThemeOverlay attribute.

Something like:

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

Currently the materialThemeOverlay attribute requires version 1.1.0 of material components for android library.

implementation 'com.google.android.material:material:1.1.0-alpha09'



回答5:


Just using AppCompatButton by setting this attribute :

"app:backgroundTint="@color/wildberries"

And make sure your activity extends AppCompatActivity. I just use it in my project. It works like a charm in both 5.X and pre-5.X.



来源:https://stackoverflow.com/questions/36261421/how-to-change-android-button-text-color-globally-in-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!