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

我现在有这个:

但我想要的是:


当前回答

带有自定义按钮;使用R.style。Widget_AppCompat_Button_Borderless, Kotlin way-

class CSButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = R.style.Widget_AppCompat_Button_Borderless
) : AppCompatButton(context, attrs, defStyleAttr)

其他回答

科特林

stateListAnimator = null

Java

setStateListAnimator(null);

XML

android:stateListAnimator="@null"

try: android:stateListAnimator="@null"

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

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

简单的

 android:elevation="0dp"

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

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