Android:ImageView xml方式配置selector 图片切换

1、在res/drawable目录下创建一个新的XML文件,比如selector_image.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/image_pressed" />

<item android:state_focused="true" android:drawable="@drawable/image_pressed" />

<item android:drawable="@drawable/image_normal" />

</selector>

2、在布局文件中使用这个Selector图片资源作为ImageView的src属性即可。例如,可以在ImageView的XML布局中添加android:src="@drawable/selector_image"来引用这个Selector图片资源。

<ImageView

android:clickable="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/selector_image"

/>

相关推荐
城东米粉儿35 分钟前
Android MediaPlayer 笔记
android
Jony_1 小时前
Android 启动优化方案
android
阿巴斯甜1 小时前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 小时前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_5 小时前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android
_小马快跑_5 小时前
Kotlin | 从SparseArray、ArrayMap的set操作符看类型检查的不同
android
_小马快跑_5 小时前
Android | 为什么有了ArrayMap还要再设计SparseArray?
android
_小马快跑_5 小时前
Android TextView图标对齐优化:使用LayerList精准控制drawable位置
android
_小马快跑_5 小时前
Kotlin协程并发控制:多线程环境下的顺序执行
android
_小马快跑_5 小时前
Kotlin协程异常捕获陷阱:try-catch捕获异常失败了?
android