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

通过xml布局文件实现按钮改变焦点设置背景图片

2014-09-28 09:09 441 查看
Button的android:background属性

    设置按钮背景图片:
        onFocus()与onClick()事件的处理
        Item的android:state_focused和android:state_pressed属性的实现

    在res文件夹下建立文件夹drawable,在drawable文件下穿件styles.xml文件,文件内容如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 失去焦点 -->
    <item android:drawable="@drawable/png_3" android:state_focused="false" android:state_pressed="true"/>
    <!-- 默认时候图片 -->
    <item android:drawable="@drawable/png_4"></item>

</selector>
    

在main.xml文件中创建组件ImageButton时候如下

    <ImageButton
        android:id="@+id/image_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:background="@drawable/styles"/>

这样,在使用ImageButton时候未点击图片按钮时候,图片显示的是png4   而点击之后显示的是png3图片,放开点击有显示默认的图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 布局
相关文章推荐