ListView 设置点击项目背景色

时间:2022-04-26 14:48:03

<strong style="color: rgb(255, 102, 102); font-family: Arial, Helvetica, sans-serif;font-size:24px;">第一步</strong>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">首先,定义一个ListViewSelectColor选择背景色,</span>

文件名:list_bg_selector

位  置  :drawable\

<?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/ItemClick"></item>
    <item android:drawable="@android:color/white"></item>
</selector>


<span style="font-family: Arial, Helvetica, sans-serif; font-size: 24px; color: rgb(255, 102, 102);"><span style="background-color: rgb(255, 255, 255);"><strong>第二步</strong></span></span>
接着,定义一个color颜色

文件名:color

位  置  :values\

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
    <color name="black">#000000</color>
    <color name="green">#00ff00</color>
    <color name="ItemClick">#F0F0F0</color>
</resources>

<span style="font-family:Arial, Helvetica, sans-serif;font-size:24px;color:#ff6666;"><span style="background-color: rgb(255, 255, 255);"><strong>第三步</strong></span></span>
定义一个ListItem

文件名:list_item

位  置  :layout\

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="center_vertical"
	android:orientation="horizontal"
	 android:background="@drawable/list_bg_selector">

	<ImageView
	    android:id="@+id/entryImageView1"
	    android:layout_width="35dp"
	    android:layout_height="35dp"
	    android:layout_marginLeft="10dp"
	    android:background="@drawable/ic_launcher" />
	
	<TextView
	    android:id="@+id/entryTextView1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:padding="10dp"
	    android:text="This Item Names"
	    android:textSize="25sp" />
	
</LinearLayout>


<span style="font-family:Arial, Helvetica, sans-serif;font-size:24px;color:#ff6666;"><span style="background-color: rgb(255, 255, 255);"><strong>第四步</strong></span></span>
设置ListView属性

<ListView
                android:id="@+id/listView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/SendMessage"
                android:background="@color/white"
                android:cacheColorHint="@android:color/secondary_text_light_nodisable"
                android:divider="@null"
                android:fastScrollEnabled="true" >


<span style="font-family: Arial, Helvetica, sans-serif; font-size: 24px; color: rgb(255, 102, 102);"><span style="background-color: rgb(255, 255, 255);"><strong>最后效果</strong></span></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 24px; color: rgb(255, 102, 102);"><span style="background-color: rgb(255, 255, 255);"><strong>
</strong></span></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 24px; color: rgb(255, 102, 102);"><span style="background-color: rgb(255, 255, 255);"><strong>
</strong></span></span>

ListView 设置点击项目背景色