Android——LinearLayout按下(state_pressed)或获取焦点时(state_focused)背景设置不同颜色或图片

来源:互联网 发布:mac office 2011 破解 编辑:程序博客网 时间:2024/06/06 02:12
在Android项目中有这样一个需求,画了几个LinearLayout布局做成ListView的样式,如下图所示:

当点击每个LinearLayout项时想改变其背景颜色,准备了如下背景图片XML文件:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@color/gray_background" android:state_pressed="true" />    <item android:drawable="@color/white_background" /></selector>
但是当我每次运行点击每一项LinearLayout时,其背景颜色毫无变化。
后来发现在每个LinearLayout项加入属性android:clickable="true"点击LinearLayout才会出现背景颜色的变化。
 
1 0
原创粉丝点击