在安卓系统中,当我将背景图像设置为一个按钮时,当它被点击时,我看不到任何效果.
我需要在按钮上设置一些效果,这样用户就可以识别按钮被点击了.
点击按钮时,按钮应暗几秒钟.怎么做?
在安卓系统中,当我将背景图像设置为一个按钮时,当它被点击时,我看不到任何效果.
我需要在按钮上设置一些效果,这样用户就可以识别按钮被点击了.
点击按钮时,按钮应暗几秒钟.怎么做?
这可以通过创建一个包含按钮状态列表的可绘制xml文件来实现.例如,如果使用以下代码创建一个名为"button.xml"的新xml文件:
<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>
要使背景图像在印刷时保持较暗的外观,请创建第二个xml文件,并将其称为渐变.包含以下代码的xml:
<?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,例如
android:background="@drawable/button"
希望这有帮助!
编辑:更改上述代码,在按钮中显示图像(YOURIMAGE),而不是块 colored颜色 .