由于没有其他答案提供自定义样式覆盖(我认为这是最安全的更新方式之一),因此我在此处发布了我的解决方案。
我发布的解决方案已经解决了新的AndroidX (support design 28) 主题。
假设您的应用程序在您的AndroidManifest.xml 中使用了一个名为MyAppTheme 的自定义它们:
<application
android:name=".MyApplicationName"
android:allowBackup="true"
android:icon="@mipmap/icon"
android:roundIcon="@mipmap/icon_round"
android:label="@string/app_name"
android:theme="@style/MyAppTheme">
创建(如果您还没有)values/style.xml 文件覆盖您的应用程序使用的主题:
<style name="MyAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/myColorPrimary</item>
<item name="colorPrimaryDark">@color/myColorPrimaryDark</item>
<item name="colorAccent">@color/myColorAccent</item>
<item name="snackbarStyle">@style/MySnackBarStyle</item>
</style>
<!-- snackbar style in res/values -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
<item name="android:background">@color/mySnackbarBackgroundColor</item>
</style>
并在您的 values/colors.xml 文件中提供您的颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myColorPrimary">#008577</color>
<color name="myColorPrimaryDark">#00574B</color>
<color name="myColorAccent">#D81B60</color>
<color name="mySnackbarBackgroundColor">#D81B60</color>
</resources>
2020 年更新
由于上述解决方案移除了小吃店的圆角,因为这样设置背景使用了传统的小吃店设计,如果您想保留材料设计,您可以。
- 如果您的目标是 API 21+
将android:background 替换为android:backgroundTint
<!-- snackbar style in res/values-21/ -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
<item name="android:backgroundTint">@color/mySnackbarBackgroundColor</item>
</style>
如果您的目标是 API res/values-21/ 文件夹中设置您的 MySnackbarStyle 以上并离开res/values 文件夹中的旧式风格。
如果您的目标是 API res/values/ 中更改您的小吃吧风格方式:
<!-- snackbar style in res/values/ -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
<item name="android:background">@drawable/my_snackbar_background</item>
</style>
从official repo借用你的my_snackbar_background,这样:
<!-- in res/drawable/ -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="4dp"/>
<solid android:color="@color/mySnackbarBackgroundColor"/>
</shape>
这是playground repo。