先上代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/library_highlight" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@drawable/library_highlight" android:state_focused="true"/>
<!-- focused -->
<!-- <item android:drawable="@drawable/button_focused" android:state_hovered="true"/>hovered -->
<item android:drawable="@drawable/bg_library_selected"/>
<!-- default -->
</selector>
以点击listview的item高亮显示为例,这个配置文件放在drawable文件夹下。
在布局中设置:直接setBackground(xxxid);
代码中设置:
Layout mLayout = findVeiwById(R.id.xxx); boolean bSelected = true or false; mLayout.setSelected(bSelected );