我该如何改变所有按钮文字的颜色?
我知道可以像下面这样设置背景颜色:
<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="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 这应该能帮助您全局更改文本颜色。
@style/Widget.AppCompat.Button
作为ButtonColor的父样式,即可获得Material Design AppCompat样式。 - levibostian使用新的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>
如果有人偶然看到这个问题,我建议查看一个关于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>
如果您只需要更改按钮的文本颜色,可以修改主题的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>
由于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>
只需设置此属性即可使用AppCompatButton
:
"app:backgroundTint="@color/wildberries"
确保你的活动继承自AppCompatActivity。我在我的项目中使用它。它在5.X和pre-5.X中都能完美运行。
<Button
app:backgroundTint="#fece2f" //any color
/>
如果您不想将其定义为全局变量,这是最佳方法