将 v21 中的 AppCompatButton 设置为没有阴影和角半径为零

Styling a AppCompatButton in v21 to have no shadow and corner radius of zero

我在 XML 布局中定义了一个 AppCompatButton,我为其设置了一个主题,如下所示:

android:theme="@style/CustomAccentOverlay"

我已经设置:

android:stateListAnimator="@null"

去除阴影。我有两个问题。

即使没有显示阴影,按钮的高度也会减去阴影的高度。我应该用其他方式去除阴影,还是如何解决这个问题?

按钮有圆角,我希望角是尖的。我无法在按钮上设置背景,因为我想保留标准的波纹效果,如果我设置背景,它就会消失(至少我不知道如果我设置背景如何保持它)。我试过设置

<item name="android:bottomLeftRadius">0dp</item>

以及 CustomAccentOverlay 主题及其相应样式的所有其他角,但它不起作用。如何在我的按钮上将角半径设置为零?

谢谢
索伦

为按钮使用以下代码。

<android.support.v7.widget.AppCompatButton
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Button"
android:stateListAnimator="@null"
android:elevation="0dp"
android:background="@android:color/darker_gray"
android:foreground="?attr/selectableItemBackground"
/>

我会解释属性。

  1. android:elevation="0dp" 和 android:stateListAnimator="@null"。按钮没有阴影。

  2. android:背景。将所需的颜色设置为背景。它删除了圆角。

  3. android:foreground="?attr/selectableItemBackground" 。按下按钮时会产生涟漪效果。

更新 1:

View 的 android:foreground 属性似乎从 API 23 开始工作。对于以下 APIs,在 drawable-v21 文件夹中创建一个带有波纹的 drawable 并将其设置为背景按钮,

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="NewApi"
    android:color="@color/ripple_color">

    <item android:drawable="@color/normal_state_button_background_color"/>

</ripple>

对于 Lollipop 之前的版本,在同名的 drawable 文件夹中创建一个带有选择器的 drawable。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed_color"
          android:state_pressed="true" />
    <item android:drawable="@drawable/focused_color"
          android:state_focused="true" />
    <item android:drawable="@drawable/normal_color" />
</selector>

First question:How to remove shadow of a button?

答案如下: 只需将此属性添加到您的按钮

android:stateListAnimator="@null"

Second question: How to make the corner of button sharp without losing the standard ripple effect. Here is the answer: But first you have to make two drawble file with same name but one for below api 21 and one for api > 21 because the ripple is only available only api > 21.So now I am showing how to create that.Read the following text carefully

右键单击 drawble 文件夹并选择新建和 "Drawble resource file" 然后单击下一步然后为 drawble 命名任何你喜欢的名称并再次按 ok.Then 右键单击​​ drawble 文件夹并选择新建和 "Drawble resource file" 并点击下一步并将 drawble 命名为你之前命名的 drawble 文件夹,但这次在底部你可以看到一个名为 "available qualifiers" 的部分。转到这个部分,在最底部你可以看到 [= 31=,点击它,然后你可以在右边看到一个箭头图标,点击它然后在 "Platform api level" 添加 21 然后按 ok.And 现在如果你展开 drawble 文件夹你可以看到两个文件您为 api 创建的 drawble file.Once 低于 21,并为您创建的上层 21.Open drawble 文件创建一次,并确保打开 [=37] 处有“(v21)” =] 从那里删除所有内容并添加以下代码

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="?attr/colorControlHighlight" xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <shape>
        <corners android:radius="0dp"/>
        <solid android:color="#D6D7D7"/>
    </shape>
</item>

</ripple>

并将此属性添加到您的按钮

android:background="@drawable/youdrawblefilethatyouhavecreated"

现在,如果您 运行 您的应用程序,您可以看到没有阴影,您的按钮有尖角,如果您单击,波纹就会出现。

最后,你的按钮看起来像这样

<android.support.v7.widget.AppCompatButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button 1"
    android:background="@drawable/yourcreatddrawblefile"
    android:stateListAnimator="@null"/>

希望对您有所帮助!

听起来您确实想要使用可点击的 TextView 而不是 Button。默认情况下,TextView 不会有阴影和尖角,您可以为其附加一个点击监听器。记住,Button 只是一个花哨的 TextView,上面有很多视觉附加组件,听起来你想删除很多。

如果要保留 TextView 上的波纹并定义自己的背景,请设置 android:foreground="?attr/selectableItemBackground"

编辑:即使其他答案被标记为已接受,我仍然认为 OP 应该使用带有点击侦听器的 TextView 并将涟漪效应应用到它而不是使用按钮。 Google I/O 应用程序实现其所有符合 Material 设计规范的平面按钮的方式正是采用这种方式的可点击 TextView。

使用此代码

 <android.support.v7.widget.AppCompatButton
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorAccent"
    android:text="@string/button" />