您的位置:首页 > 移动开发 > Android开发

Android 中listview点击一个item时背景色的设置

2015-11-07 12:37 603 查看
1.在values中添加一个colors的xml文件应于调用。

2.在drawable中添加一个item_selector.xml文件,用于供listview调用。

3.在listview中进行对点击时背景改变时调用。

colors.xml文件:<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#00000000</color>// 透明的 当默认的时候也就是不进行点击事件的时候进行调用。
<color name="choose">#9fb01513</color>//当点击时需要显示的颜色。
</resources>
item_selector.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

//默认不被按压的状态
<item android:drawable="@color/transparent" android:state_pressed="false"/>
//按钮被按压下的状态
<item android:drawable="@color/choose" android:state_pressed="true"/>
</selector>在listview中设置的值:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/lv_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<span style="color:#FF0000;">android:listSelector="@drawable/item_selector"</span> //对颜色的改变的调用。
/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: