使用
Material Components Library,您可以在应用程序主题中
全局更改 Snackbar 样式:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<!-- Style to use for Snackbars in this theme. -->
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
<!-- Style to use for action button within a Snackbar in this theme. -->
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
<!-- Style to use for message text within a Snackbar in this theme. -->
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
....
</style>
注意:
snackbarStyle
和snackbarButtonStyle
需要版本1.1.0
snackbarTextViewStyle
需要版本1.2.0
.
例如:
<style name="snackbar_style" parent="@style/Widget.MaterialComponents.Snackbar">
<item name="android:layout_margin">32dp</item>
</style>
<style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">
<item name="backgroundTint">@color/secondaryLightColor</item>
<item name="android:textColor">@color/primaryDarkColor</item>
</style>
<style name="snackbar_text" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
<item name="android:textColor">@color/secondaryLightColor</item>
</style>
![enter image description here](https://istack.dev59.com/z5iAY.webp)
这只是一个示例,演示如何同时更改操作按钮的父样式。您可以使用标准的Widget.MaterialComponents.Button
:
<style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button">
<item name="backgroundTint">@color/secondaryLightColor</item>
<item name="android:textColor">@color/primaryDarkColor</item>
</style>
要更改 SnackBar
的< strong>背景颜色,您可以使用:
<style name="snackbar_style" parent="@style/Widget.MaterialComponents.Snackbar">
<!-- using backgroundTint the alpha layer is ignored -->
<item name="backgroundTint">@color/....</item>
</style>
或者如果您更喜欢:
<style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
<item name="materialThemeOverlay">@style/snackbar_overlay</item>
<!-- If you want to avoid the alpha level for the color that is overlaid on top of the background color-->
<item name="backgroundOverlayColorAlpha">1.0</item>
</style>
<style name="snackbar_overlay">
<item name="colorOnSurface">....</item>
</style>