您的位置:首页 > 移动开发 > Android开发

控件按钮的点击效果

2012-09-11 09:08 148 查看
手机上按钮通常点击时显示一种效果,没有点击时是另一种效果,这也是默认的情况。但是如果你使用图片按钮,并把背景色设成#00000000后,点击后的效果就看不到了,对于用户来说很不友好,所以你需要做一些修改:使用两张图片,一张是正常显示,一张是点击后的效果。这也是为什么打开别人的res文件夹后总会看到相同前缀名的图片资源的原因。

下面说一下怎么实现的:

1.新建一个drawable文件夹,拷贝icon_play_prev_normal.png和icon_play_prev_pressed.png两张图片进去。

2.还在上面的文件夹中编写icon_play_prev.xml该文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/icon_play_prev_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/icon_play_prev_normal"/>

</selector>


3.在layout文件夹中编写需要的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" >

<ImageButton
android:background="#00000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_play_prev" />

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息