应用中用到了listview,自己写了listitem,在设置item背景上一直没有成功,不管是在layout中设置背景色还是在activity中设置背景色,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:background="@drawable/my_drawable" android:orientation="horizontal" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="22px" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5px" android:textColor="#000000" android:textSize="16px" /> </LinearLayout> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:focusable="false" /> </LinearLayout>
my_drawable.xml文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- focused --> <item android:drawable="@color/white" android:state_focused="true"/> <!-- focused and pressed --> <item android:drawable="@color/white" android:state_focused="true" android:state_pressed="true"/> <!-- pressed --> <item android:drawable="@color/white" android:state_pressed="true"/> <!-- default --> <item android:drawable="@color/white"/> </selector>
color.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#FFFFFF</color> </resources>
这样listview的item背景色就变成白的啦