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

我现在有这个:

但我想要的是:


当前回答

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

android:stateListAnimator="@null"

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

其他回答

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

<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"/>

科特林

stateListAnimator = null

Java

setStateListAnimator(null);

XML

android:stateListAnimator="@null"

try: android:stateListAnimator="@null"

以上所有答案都很棒,但我将建议另一种选择:

<style name="FlatButtonStyle" parent="Base.Widget.AppCompat.Button">
 <item name="android:stateListAnimator">@null</item>
 <!-- more style custom here --> 
</style>


我使用自定义样式

<style name="MyButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless"></style>

别忘了加上

<item name="android:textAllCaps">false</item>

否则按钮文本将是大写的。