如何去除按钮阴影 (android)

How to remove button shadow (android)

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

我现在有这个:

但我想要这个:

将其用作按钮的背景可能会有所帮助,请根据需要更改颜色

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <solid android:color="@color/app_theme_light" />
            <padding
                android:left="8dp"
                android:top="4dp"
                android:right="8dp"
                android:bottom="4dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/app_theme_dark" />
            <padding
                android:left="8dp"
                android:top="4dp"
                android:right="8dp"
                android:bottom="4dp" />
        </shape>
    </item>
</selector>

另一种选择是添加

style="?android:attr/borderlessButtonStyle"

到您的按钮 xml,如此处所述 http://developer.android.com/guide/topics/ui/controls/button.html

一个例子是

<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
style="?android:attr/borderlessButtonStyle" />

我使用自定义样式

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

别忘了加

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

否则按钮文本将为大写。

尝试:android:stateListAnimator="@null"

一种更简单的方法是将此标记添加到您的按钮中:

android:stateListAnimator="@null"

尽管它需要 API 21 级或更高..

@Alt-Cat 的回答对我有用!

R.attr.borderlessButtonStyle 不包含阴影。

而且按钮的文档很棒。

此外,您可以在第二个构造函数中的自定义按钮上设置此样式。

    public CustomButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.borderlessButtonStyle);
    }

Kotlin

stateListAnimator = null

Java

setStateListAnimator(null);

XML

android:stateListAnimator="@null"

您可以使用 TextView 代替 Button,并在 java 代码中添加点击侦听器。

在 activity 布局中 xml:

<TextView
    android:id="@+id/btn_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:text="@string/btn_text"
    android:gravity="center"
    android:textColor="@color/colorAccent"
    android:fontFamily="sans-serif-medium"
    android:textAllCaps="true" />

在 activity java 文件中:

TextView btnTextView = (TextView) findViewById(R.id.btn_text_view);
btnTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // handler code
            }
        });

Material 设计按钮添加到按钮 xml: style="@style/Widget.MaterialComponents.Button.UnelevatedButton"

以上所有答案都很好,但我会建议其他选项:

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


带有自定义按钮;使用样式 R.style.Widget_AppCompat_Button_Borderless,Kotlin 方式-

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

简单

 android:elevation="0dp"