如何删除按钮阴影(android)

时间:2015-02-27 01:31:25

标签: android button shadow

我想从按钮上移除阴影,使其看起来更平坦。

我现在有这个:

cancel button with shadow

但我想要这个:

cancel button without shadow

11 个答案:

答案 0 :(得分:299)

另一种选择是添加

style="?android:attr/borderlessButtonStyle"

到您的Button 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" />

答案 1 :(得分:81)

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

android:stateListAnimator="@null"

虽然它需要API等级21或更高..

答案 2 :(得分:42)

我使用自定义样式

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

答案 3 :(得分:29)

<强>爪哇

setStateListAnimator(null);

<强> XML

android:stateListAnimator="@null"

答案 4 :(得分:21)

尝试:android:stateListAnimator="@null"

答案 5 :(得分:6)

将此作为按钮的背景可能有所帮助,可根据需要更改颜色

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

答案 6 :(得分:3)

@ Alt-Cat回答我的工作!

R.attr.borderlessButtonStyle不包含阴影。

按钮文件很棒。

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

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

答案 7 :(得分:3)

您可以使用TextView并在Java代码中添加单击侦听器,而不是Button。

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

在活动java文件中:

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

答案 8 :(得分:2)

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

答案 9 :(得分:1)

以上所有答案都是不错的选择,但我会建议另一个选择:

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


答案 10 :(得分:-3)

diff <(hg files "set:symlink()" -r.) <(hg files "set:symlink()")

将此添加到您的活动中也可以为您提供帮助。

相关问题