Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何移除按钮阴影(android)_Android_Button_Shadow - Fatal编程技术网

如何移除按钮阴影(android)

如何移除按钮阴影(android),android,button,shadow,Android,Button,Shadow,我想去掉按钮上的阴影,使它看起来更平 我现在有这个: 但我想要这个: 使用此按钮作为背景可能有助于根据需要更改颜色 <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape andr

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

我现在有这个:

但我想要这个:


使用此按钮作为背景可能有助于根据需要更改颜色

<?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,如本文所述

例如

<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>
false

否则按钮文本将使用大写。

尝试:
android:stateListAnimator=“@null”
一种更简单的方法是将此标记添加到按钮:

android:stateListAnimator="@null"

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

但@Alt Cat答案对我来说很有用

R.attr.borderlessButtonStyle不包含阴影

button的文档很棒

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

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

Kotlin

stateListAnimator = null

Java

setStateListAnimator(null);

XML

android:stateListAnimator="@null"

您可以使用TextView而不是按钮,并在java代码中添加click侦听器

在活动布局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" />

材料设计按钮添加到按钮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>



@空的


我在这里写评论,这是我的工作。[ [1]:这将删除按钮样式,例如阴影和浮雕。添加带有形状的选择器是可选的。这是一个更准确的答案。此外,我为按钮使用自定义样式。您可以从以下内容扩展自定义样式:您能解释一下这是如何工作的吗?它没有为我删除阴影。它将原始背景替换为阴影,w使用纯色背景虽然这可以解决OP的问题,但我建议添加一些背景。为什么会有帮助?还有,“试试这个”有点误导。你确定这会解决问题,还是只是猜测?如果是这样,你应该写一条评论。它需要API 21。简单明了。需要API 21级或更高级别。设置后stateListAnimator to null按钮不可见,有人遇到了这一点吗?这不仅仅是阴影。我非常喜欢它方法更好,它更灵活。@Alon Kogan,使用
android:stateListAnimator
属性是否有向后兼容性?此解决方案允许您创建一个样式机器人,它必须继承
borderlessButtonStyle
样式,这给了您更多的灵活性。这是唯一适合我的解决方案,无边界ButtonStyle标签不起作用,因为我需要在按下按钮时去除阴影这不是正确的答案,但你实际上帮助我解决了一个类似的问题,所以谢谢!啊哈!好主意--给了我一个快速解决方案--它类似于Alon Kogan()。这与上面的5ish有什么不同?有时类似,但处于禁用状态的灰色按钮会变亮。
<style name="FlatButtonStyle" parent="Base.Widget.AppCompat.Button">
 <item name="android:stateListAnimator">@null</item>
 <!-- more style custom here --> 
</style>