如何去除 Dialog 上 Button 的阴影?为什么显示阴影?

How can I remove shadow of Button on Dialog? Why is the shadow shown?

我创建了一个对话框并且有一个按钮。

我想要不同风格的按钮。所以我做了这个文件并应用到按钮的背景属性。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/my_pink_color">
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/btn_line_border_dialogue"></item>
</ripple>

我不知道为什么按钮周围有阴影。它有两个按钮。而第二个按钮没有任何东西。当我移除第二个按钮时,阴影似乎消失了。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:clickable="true"
        android:background="@drawable/btn_ripple"
        android:text="Close"
        android:textAllCaps="false"
        android:textColor="@color/my_text_color"
        android:textSize="17sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_close2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_ripple"
        android:clickable="true"
        android:text="Don't show this in 24hours"
        android:textAllCaps="false"
        android:textColor="@color/my_text_color"
        android:textSize="14sp"
        android:textStyle="bold" />
</LinearLayout>

当我只是应用 btn_line_border_dialogue.xml 时没有波纹。是一样的。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/my_yellow" />
    <stroke
        android:width="1dp"
        android:color="@color/my_pink" />
</shape>

如何消除这种影响?

尝试将高度设置为 t0 0 dp,或者您可以使用 TextView 或 Layout 代替按钮。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/btn_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:clickable="true"
        android:background="@drawable/btn_ripple"
        android:text="Close"
        android:textAllCaps="false"
        android:textColor="@color/my_text_color"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/btn_close2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_ripple"
        android:clickable="true"
        android:text="Don't show this in 24hours"
        android:textAllCaps="false"
        android:textColor="@color/my_text_color"
        android:textSize="14sp"
        android:textStyle="bold" />
</LinearLayout>

这也可以通过在 Resources\values\styles 中全局设置 android:stateListAnimator="@null" 来解决。xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="AppTheme" parent="AppTheme.Base">
    </style>
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:buttonStyle">@style/NoShadowButton</item>
    </style>
    <style name="NoShadowButton" parent="android:style/Widget.Button">
        <item name="android:stateListAnimator">@null</item>
    </style>
</resources>

Button 中使用 style="?android:attr/borderlessButtonStyle" 参考:https://developer.android.com/guide/topics/ui/controls/button.html#Borderless 或者,如果您不需要特别 Button,请使用 TextView 代替

在 xml 中将按钮上的 stateListAnimator 设置为 = "@null"。它会移除按钮的阴影。