问题
I want to simply apply styling to all buttons on a theme level like this
<style name="BaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="buttonStyle">@style/DefaultButton</item>
</style>
<style name="DefaultButton" parent="Widget.MaterialComponents.Button">
<item name="android:textColor">@color/whatever</item>
</style>
<Button
android:id="@+id/addChannelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="16dp"
android:text="Add room" />
Why doesnt this work? It would in appcompat
// If I use Theme.MaterialComponents.Light.NoActionBar.Bridge
, then it works
回答1:
You should be setting materialButtonStyle
instead of buttonStyle
in your theme.
回答2:
Use a Theme.MaterialComponents theme defining the materialButtonStyle attribute.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="materialButtonStyle">@style/MyButtonTheme</item>
</style>
In this way you can customize the theme of all the buttons in your app.
Also starting from version 1.1.0 of the material library 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="colorOnPrimary">@color/my_color</item>
</style>
Currently it requires version 1.1.0 of material components for android library.
回答3:
Can you try with v1.0.0-alpha06? There has been some progress since v1.0.0 which may have addressed what you're seeing.
1.1.0-alpha02
- Shape Theming: FloatingActionButton, MaterialButton, Chip, MaterialCardView, BottomSheet, & TextInputLayout updated to use the new Material shape system
1.1.0-alpha06
- Implement Shapeable interface in MaterialButton
Source: https://github.com/material-components/material-components-android/releases
回答4:
Use materialButtonStyle
and use MaterialButton
instead of Button
来源:https://stackoverflow.com/questions/56296694/material-design-components-button-global-theme-overriding-broken