在v21中设置AppCompatButton的样式,使其阴影和角半径不为零 [英] Styling a AppCompatButton in v21 to have no shadow and corner radius of zero

查看:637
本文介绍了在v21中设置AppCompatButton的样式,使其阴影和角半径不为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML布局中定义了一个AppCompatButton,并且为此设置了一个主题:

I have a AppCompatButton defined in a XML layout, and I have set a theme for it like this:

android:theme="@style/CustomAccentOverlay"

我已经设置:

android:stateListAnimator="@null"

以去除阴影.我对此有两个问题.

to remove the shadow. I have two problems with this.

即使未显示阴影,按钮的高度也会减去阴影的高度.我应该以其他方式去除阴影,还是要解决此问题?

The height of the button is deducted the height of the shadow, even though the shadow is not shown. Should I remove the shadow in some other way, or how do I fix this?

该按钮具有圆角,我希望该角变尖.我无法在按钮上设置背景,因为我想保持标准波纹效果,但是如果设置背景,该效果就会消失(至少,如果设置背景,我将不知道如何保持该效果).我尝试设置

The button has rounded corners, and I want the corners to be sharp. I can not set a background on the button, because I want to keep the standard ripple effect and that goes away if I set a background (at least I don't know how to keep it if I set a background). I have tried setting

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

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

and all the other corners to the CustomAccentOverlay theme and also its corresponding style, but it does not work. How can I set the corner radius to zero on my button?

谢谢
索伦

推荐答案

对Button使用以下代码.

Use the following code for the Button.

<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".按钮没有阴影.

  1. android:elevation="0dp" and android:stateListAnimator="@null". No shadow for the button.

android:background.将所需的颜色设置为背景.它将消除圆角.

android:background . Set the desired color as background. It removes the rounded corners.

android:foreground =?attr/selectableItemBackground".按下按钮时会产生波纹效果.

android:foreground="?attr/selectableItemBackground" . It gives the ripple effect when the button is pressed.

更新1:

View的android:foreground属性似乎可以从API 23开始工作.对于以下API,请在drawable-v21文件夹中创建带有涟漪效果的drawable并将其设置为按钮的背景,

It seems like android:foreground attribute for View works from API 23. For below APIs, create a drawable with ripple in drawable-v21 folder and set it as background to the button,

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

For pre Lollipop versions, create a drawable with selector in drawable folder with the same name.

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

这篇关于在v21中设置AppCompatButton的样式,使其阴影和角半径不为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆