我想去掉按钮的阴影,使它看起来更平坦。

我现在有这个:

但我想要的是:


当前回答

它要求API级别为21

        android:outlineProvider="none"

其他回答

try: android:stateListAnimator="@null"

简单的

 android:elevation="0dp"

一个更简单的方法是添加这个标签到你的按钮:

android:stateListAnimator="@null"

尽管它需要API级别21或更高。

更新材料组件

如果你之前使用过这样的东西:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:buttonStyle">@style/NoShadowButton</item>
</style>

<style name="NoShadowButton" parent="Widget.AppCompat.Button">
    <item name="android:stateListAnimator">@null</item>
</style>

和我一样,你也在挠头,想知道为什么你已经迁移到了Theme.MaterialComponents.Light。*,这是因为buttonStyle只引用AppCompat按钮样式-有一个新的样式materialButtonStyle。这解决了我的问题:

<style name="MyTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="materialButtonStyle">@style/Widget.MaterialComponents.Button.UnelevatedButton</item>
</style>

这里有一个很好的指南,告诉您还有哪些已经更改,以及如何正确迁移。

这里没有提到,但如果你想要一个没有阴影的材质按钮,你可以通过创建以下自定义样式来实现:

<style name="materialButtonNoShadow" parent="Widget.MaterialComponents.Button.UnelevatedButton">

然后把它应用到你的xml布局中:

            <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/noShadowButton"
            style="?attr/materialButtonNoShadow"/>