Item background color setting for Android listview

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.