Android Art--listview change the item background color when selected

Source: Internet
Author: User

By default, the ListView background color is black, the highlighted color of the item selected is Chrysanthemum Yellow, and many times you have to define the background color or background image.

android:cachecolorhint= "@android: Color/transparent", meaning to go black background, such as the ListView when scrolling will refresh the interface, the default color or system color, So set it to transparent in this way, which is useful when you use a fillet picture in a ListView to set up a ListView

android:divider= "@null" is used to remove the black line between the ListView item

1. Background color

That is, by setting color in list_item_color_bg.xml to achieve different colors when clicking on item, but if you use color, the ListView cannot use the Android:listselector property, if you set Android : Listselector mode, if you click on an item, the whole ListView will become a color, then you must use the way you set android:background in item. Android:listselector mode applies to pictures in a way that is similar to (android:drawable= "@drawable/img")

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

Color.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources> <color name=    "White" > #ffffff </color>    <color name= "Black" > #000000 </color>    <color name= "green" > #00ff00 </color></ Resources>

Now look at the layout file.

Listview.xml, in a color way, you can't use listselector here.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    >    <listview         android:id=" @+id/lv "        android:layout_width=" Fill_parent        " android:layout_height= "Wrap_content"        android:fastscrollenabled= "true"        android:cachecolorhint= "@ Android:color/transparent "        android:divider=" @null "        /></linearlayout>

List_item_color.xml, set the background directly in the item's layout by using the color settings

<?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=" Horizontal "android:background= "@drawable/list_item_color_bg" ><imageview android:id= "@+id/img" android:layout_width= "wrap_content" android:layout_height= "Wrap_c Ontent "/> <linearlayout android:layout_width=" fill_parent "android:layout_height=" Wrap_con Tent "android:orientation=" vertical "> <textview android:id=" @+id/tv "and        Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "20SP" /> <textview android:id= "@+id/info" android:layout_width= "Wrap_content" an droid:layout_height= "Wrap_content" android:textsize= "14sp"/> </linearlayout></linear Layout>

2. Background map

This approach is to use the picture in the selector file to set the background of item, whether it is to set the android:listselector of the ListView or the way to set the android:background of the item, However, it is best to use Android:background, as the default picture is set in the selector file when Android:listselector is used in the following way

(<item android:drawable= "@drawable/login_input"/>) will not be displayed, but instead background in the same way. It's weird, I want to know.

<?xml version= "1.0" encoding= "Utf-8"? ><selector  xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" true "android:drawable=" @drawable/input_over "/>    <item android:drawable= "@drawable/login_input"/></selector>

The ListView is now set as follows, where Android:background is not set in item

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    >    <listview         android:id=" @+id/lv "        android:layout_width=" Fill_parent        " android:layout_height= "Wrap_content"        android:fastscrollenabled= "true"        android:cachecolorhint= "@android : Color/transparent "        android:listselector=" @drawable/list_item_drawable_bg "        /></ Linearlayout>

Here's the following: the background image is. 9.png picture, note the default white. 9.png picture Login_input not shown

If you use Android:background way, cancel the android:listselector way, the effect is as follows

Http://www.cnblogs.com/loulijun/archive/2012/04/15/2450312.html

Android Art--listview change the item background color when selected

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.