【问题标题】:Click effect in AndroidAndroid中的点击效果
【发布时间】:2015-04-17 17:48:59
【问题描述】:
我有一些设置按钮背景的代码,但是当我运行代码时,我没有看到单击按钮的效果!下面,我向您展示我的背景 xml 文件。谢谢!
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="5dp"
>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffe49713">
</solid>
</shape>
</item>
</layer-list>
【问题讨论】:
标签:
android
xml
button
background
【解决方案1】:
这可以通过创建一个包含按钮状态列表的可绘制 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 文件并使用以下代码将其命名为 gradient.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)而不是块颜色。
希望以上描述对您有所帮助。
如果您需要我方的更多帮助,请告诉我。
【解决方案2】:
尝试将此添加到按钮的背景中。根据您的要求更改颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="@android:color/holo_red_dark"/>
<item android:state_focused="true" android:state_pressed="true" android:color="#ffffff"/>
<item android:state_focused="false" android:state_pressed="true" android:color="#ffffff"/>
<item android:color="@android:color/holo_red_dark"/>
</selector>