016 Android 图片选择器(在选中和未选中的过程中,切换展示图片)

1.目标效果

在选中和未选中的过程中,切换展示图片

2.实现方法

(1)在app--->res--->drawable

右击drawable文件夹右键,new ---->drawable resource file

注意:生成的xml文件必须在drawable目录下,而不是在drawable(v24)包下。

(2)编写xml文件

实例:selector_nextbtn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--选中用深绿色的图-->
    <item android:state_pressed="true" android:drawable="@drawable/function_greenbutton_pressed"></item>
    <!--未选中用浅绿色的图-->
    <item android:drawable="@drawable/function_greenbutton_normal"></item>
</selector>

(3)图片选择器的使用

往往通过设置控件的 android:background 属性。

<Button
            android:text="下一页"
            android:background="@drawable/selector_nextbtn_bg"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

3.效果图

观察按钮颜色的变化。

参考文献:https://developer.android.google.cn/guide/topics/resources/drawable-resource#StateList

 

posted @   雨后观山色  阅读(1150)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示