1. How to change the background color of item and press the color
ListView By default, the background color of item is black, which is yellow when the user clicks it. If you need to modify the background color to be customized, there are three ways to do it generally:
1) Set Listselector
2) Set the background of item in the layout file
3) set in the GetView of adapter
These three methods can be changed to change the default background color and press the color, the following to explain separately, but before this need to write a good selector.xml file;
<? XML version= "1.0" encoding= "Utf-8" ?> < selector xmlns:android = "Http://schemas.android.com/apk/res/android" > < android:state_pressed= "true" android:drawable= "@color/light_ Blue "></item> <android: state_pressed= "false" android:drawable= "@color/sgray"></ Item></selector>
You can use selector to change the default background color of button or ListView item. Drawable can be set as a color resource or as a picture resource.
1) Set the Listselector of the ListView
< ListView Android:id = "@+id/history_list" android:layout_width= "Fill_parent" android:layout_height= "Wrap_ Content " android:divider=" #565C5D " android:dividerheight = "3DP" android:listselector= "@drawable/selector" android: Cachecolorhint= "@android: color/transparent"></ListView >
2) Set the Background property in the ListItem layout file, the following is the ListItem layout file
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:background= "@drawable/selector"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "History"Android:textcolor= "#ffffff"android:textsize= "20SP"android:layout_centerinparent= "true"> </TextView></Relativelayout>
3) Set in the GetView method of the adapter
if (Convertview = =null) {null);} convertview.setbackgroundresource ( R.drawable.selector);
The above method can achieve the same effect, is to change the item default background color and click on the background color, the third method is the most flexible, if the ListView odd and even rows need to be set to a different selector, can only use the third method.
Reference http://www.cnblogs.com/dolphin0520/p/3383073.html