Android settings listview Item Check background color

Source: Internet
Author: User

ListView is a common Android control, when clicked on the ListView item, the orange background color is displayed by default, and the corresponding color is displayed when tumbling. This tends to be very uncoordinated with the actual software UI design style. By setting the ListView background color, the implementation is compatible with the software UI style.

Changing the listview background option often takes the form of creating an XML file, such as Listview_bg.xml, which defines the relevant properties of the selector, puts the file in the Drawable resource file when the resource file is used in the ListView The item configures the Background property android:background= "@drawable/listview_bg" to achieve the purpose of changing the background color.

But the problem is that the setting of the android:background= "@drawable/listview_bg" property is a drawable resource file, which means Listview_ Bg.xml configuration drawable requires a resource file that corresponds to a picture, but often requires only color code rather than picture resources. This time you need to configure drawable in Listview_bg.xml by referencing a color resource file, i.e. android:drawable= "@color/white", so that you do not need to reference a similar android:drawable= "@ Drawable/image "Such a picture file.

The following are the relevant code files.

Listview_bg.xml (background color state setting)

View Plaincopy to Clipboardprint?<?xml version= "1.0" encoding= "Utf-8"?>  <selector xmlns:android= "http:/ /schemas.android.com/apk/res/android ">  <!--background color without focus--  <item android:state_window_focused = "false"  android:drawable= "@color/unfocused"/>  <!--get focus in non-touch mode and click on background color-  < Item android:state_focused= "True" android:state_pressed= "true"  android:drawable= "@color/pressed"/ >  < background color when clicked!--Touch mode-  <item android:state_focused= "false" android:state_pressed= " True "  android:drawable=" @color/white "/>  <!--background color When selected-  -  <item Android:state_selected= "true"  android:drawable= "@color/selected"/>  <!--background color when getting focus--  <item android:state_focused= "true" android:drawable= "@color/focused"/>  </selector>  

Color.xml (color configuration file)

View Plaincopy to Clipboardprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <color name="white"> #ffffffff</color>
  4. <color name="unfocused"> #cccccccc</color>
  5. <color name="pressed"> #fff22fff</color>
  6. <color name="selected"> #fff33fff</color>
  7. <color name="focused"> #ffff44ff</color>
  8. </Resources>

Item.xml (ListView Item Option Layout file)

View Plaincopy to Clipboardprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <!--items Options--
  3. <relativelayout
  4. android:id="@+id/relativelayout"
  5. android:layout_width="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. android:layout_height="wrap_content"
  8. android:paddingbottom="4dip"
  9. android:paddingleft="12dip"
  10. android:paddingright="12dip"
  11. android:background="@drawable/listview_bg"
  12. >
  13. <ImageView
  14. android:paddingtop="22dip"
  15. android:layout_alignparentright="true"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:id="@+id/more_image"
  19. />
  20. <TextView
  21. android:layout_height="wrap_content"
  22. android:textsize="18dip"
  23. android:layout_width="fill_parent"
  24. android:id="@+id/title"
  25. android:paddingtop="6dip"
  26. />
  27. <TextView
  28. android:text=""
  29. android:layout_height="wrap_content"
  30. android:layout_width="fill_parent"
  31. android:layout_below="@+id/title"
  32. android:id="@+id/date"
  33. android:paddingright="20dip"
  34. />
  35. </relativelayout>

Main.xml (ListView file)

View Plaincopy to Clipboardprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ListView android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:divider="@color/white"
  10. android:dividerheight="1dip"
  11. android:id="@+id/list_items"
  12. />
  13. </linearlayout>

Selectoractivity.java (Java source file)

View Plaincopy to Clipboardprint?
  1. Package com.test.main;
  2. Import java.util.ArrayList;
  3. Import Java.util.HashMap;
  4. Import android.app.Activity;
  5. Import Android.os.Bundle;
  6. Import Android.widget.ListView;
  7. Import Android.widget.SimpleAdapter;
  8. Public class Selectoractivity extends Activity {
  9. /** Called when the activity is first created. * /
  10. @Override
  11. public void OnCreate (Bundle savedinstancestate) {
  12. super.oncreate (savedinstancestate);
  13. Setcontentview (R.layout.main);
  14. ListView list= (ListView) Findviewbyid (R.id.list_items);
  15. arraylistnew arraylist
  16. String []title={"Apple","Google","Facebook"};
  17. String []date={"2-12","5-16","9-12"};
  18. For (int i = 0; i < 3; i++)
  19. {
  20. hashmap<string, object> map = new hashmap<string, object> ();
  21. Map.put ("More_image", R.drawable.more); ID of the image resource
  22. Map.put ("title", Title[i]);
  23. Map.put ("date", Date[i]);
  24. Listitem.add (map);
  25. }
  26. Data source
  27. Simpleadapter listitemadapter= New Simpleadapter (selectoractivity. This, ListItem,
  28. R.layout.item,//ListItem XML Implementation
  29. //dynamic array with imageitem corresponding to the child
  30. New string[] { "more_image", "title", "date"},
  31. //XML file inside a ImageView, two x TextView ID
  32. new int[] {r.id.more_image, r.id.title,
  33. R.id.date});
  34. List.setadapter (Listitemadapter);
  35. }
  36. }

At last

Figure-1 Click on the background color

Fig. 2 Background color when tumbling

Android settings listview Item Check background color

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.