点击在Android的按钮效果 [英] click effect on button in Android

查看:122
本文介绍了点击在Android的按钮效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的同时,我设置背景图片按钮我看不到按钮任何影响。 我需要按钮有一定的影响,以便用户能够识别被点击的按钮。 巴顿应该是黑暗的,而几秒钟,当点击,所以我应该怎么做呢?

in android while I set background image to Button I can not see any effect on button. I need some effect on button so user can recognize that button is clicked. Button should be dark while a few second when it clicked , so what should I do for this ?

推荐答案

这可以通过创建一个包含状态的按钮列表的绘制xml文件来实现的。因此,例如,如果你创建了一个名为button.xml具有以下code新的XML文件:

This can be achieved by creating a drawable xml file containing a list of states for the button. So for example if you create a new xml file called "button.xml" with the following code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/YOURIMAGE" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
    <item android:drawable="@drawable/YOURIMAGE" />
</selector>

要保持背景图像上preSS黑暗的外观,创建第二个XML文件,并调用它gradient.xml具有以下code:

To keep the background image with a darkened appearance on press, create a second xml file and call it gradient.xml with the following code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/YOURIMAGE"/>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
        </shape>
    </item>
</layer-list>

在您的按钮设定背景为按钮XML如XML

In the xml of your button set the background to be the button xml e.g.

android:background="@drawable/button"

希望这有助于!

Hope this helps!

编辑:改变了上述code在按钮显示的图像(YOURIMAGE),而不是一个块色

Changed the above code to show an image (YOURIMAGE) in the button as opposed to a block colour.

这篇关于点击在Android的按钮效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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