如同点击button一样,实现点击listview
第一步:在drawable文件夹中新建list_view_selected样式文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@android:drawable/list_selector_background" /> <item android:state_enabled="true" android:state_pressed="true" android:drawable="@android:drawable/list_selector_background" /> <item android:state_enabled="true" android:state_checked="true" android:drawable="@android:drawable/list_selector_background" /> <item android:drawable="@drawable/wb_item_list_bg" /> </selector> <!-- drawable="@android:drawable/list_selector_background"这个为系统自带的点击的状态 drawable="@drawable/wb_item_list_bg" 这个为点击后的颜色 这个文件主要是编写点击listview中一个子控件,类似于点击button -->
第二部:在values中新建drawable文件(里面主要是设置点击后显示的图片颜色)
<?xml version="1.0" encoding="utf-8"?> <resources> <item type="drawable" name="wb_item_list_bg">#d5f8f5</item> </resources> <!-- 这里是编写点击listview中控件后,显示的颜色-->
第三部:设置listview的背景为list_view_selected
<?xml version="1.0" encoding="utf-8"?> <!--发表动态评论转发页面 通过Adapter,data将这个模板展示在listview中--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/list_view_selected">
第四部:设置listview的属性
<ListView android:id="@+id/list_view_home" android:layout_height="wrap_content" android:layout_width="match_parent" android:dividerHeight="1dp" android:cacheColorHint="#000000" /><!--android:divider="@drawable/line"-->
//
android:dividerHeight="1dp" android:cacheColorHint="#000000"
第一个为分割线的高度,第二个必须写,这是设置listview的背景为图片时,设置这个android:cacheColorHint="#000000"为透明,就可以实现点击后的功能