State List
GridView点击的背景,默认: 改成透明,配置属性 android:listSelector="@android:color/transparent" |
一、State List使用 详细查看官方文档: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList 1)在res目录下建立drawable目录,里面建立某个组件的自定义状态选择器xml文件 XML file saved at res/drawable/home_item_states_selector.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="@color/gray" /> <!-- pressed --> 按下图标
<item android:state_focused="true"
android:drawable="@color/gray" /> <!-- focused --> 图标聚焦
<item android:state_hovered="true"
android:drawable="@color/gray" /> <!-- hovered -->
<item android:drawable="@android:color/transparent" /> <!-- default -->默认不点击的效果
</selector>2)res/values/mycolor.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#44ff0000</color>
<color name="green">#5500ff00</color>
<color name="blue">#660000ff</color>
<color name="gray">#33000000</color>
</resources> 3)res/values/grid_home_item.xml中为
LinearLayout 设置背景
android:background="@drawable/home_item_states_selector" 使用自定义状态选择器点击效果 |
二、也可以使用StateList指定图片文件 a) 在res目录下建立drawable目录,里面建立某个组件的自定义状态选择器xml文件,advanced_tools_pressed_seletcor.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/advance_tools_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/advance_tools_pressed" /> <!-- focused -->
<item android:drawable="@drawable/advance_tools" /> <!-- default -->
</selector> 注意:state_pressed表示被按下,state_focused背时被聚焦,android:drawable这个表示默认,一般为图标的原始样子, 如果配置成 <item android:drawable="@android:color/transparent" /> ,那么将是透明的。 b) HomeActivity.java中设置 private int[] icons = { R.drawable.safe_theft, R.drawable.callmsg_guard,
R.drawable.app_manager, R.drawable.task_manager,R.drawable.net_manager,
R.drawable.phone_antivirus,R.drawable.system_optimize,
R.drawable.advanced_tools_pressed_seletcor,
R.drawable.app_settings };点击前: 点击后: |