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?
- <? XML version= "1.0" encoding="Utf-8"?>
- <resources>
- <color name="white"> #ffffffff</color>
- <color name="unfocused"> #cccccccc</color>
- <color name="pressed"> #fff22fff</color>
- <color name="selected"> #fff33fff</color>
- <color name="focused"> #ffff44ff</color>
- </Resources>
Item.xml (ListView Item Option Layout file)
View Plaincopy to Clipboardprint?
- <? XML version= "1.0" encoding="Utf-8"?>
- <!--items Options--
- <relativelayout
- android:id="@+id/relativelayout"
- android:layout_width="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingbottom="4dip"
- android:paddingleft="12dip"
- android:paddingright="12dip"
- android:background="@drawable/listview_bg"
- >
- <ImageView
- android:paddingtop="22dip"
- android:layout_alignparentright="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/more_image"
- />
- <TextView
- android:layout_height="wrap_content"
- android:textsize="18dip"
- android:layout_width="fill_parent"
- android:id="@+id/title"
- android:paddingtop="6dip"
- />
- <TextView
- android:text=""
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_below="@+id/title"
- android:id="@+id/date"
- android:paddingright="20dip"
- />
- </relativelayout>
Main.xml (ListView file)
View Plaincopy to Clipboardprint?
- <? 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:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:divider="@color/white"
- android:dividerheight="1dip"
- android:id="@+id/list_items"
- />
- </linearlayout>
Selectoractivity.java (Java source file)
View Plaincopy to Clipboardprint?
- Package com.test.main;
- Import java.util.ArrayList;
- Import Java.util.HashMap;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.widget.ListView;
- Import Android.widget.SimpleAdapter;
- Public class Selectoractivity extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- ListView list= (ListView) Findviewbyid (R.id.list_items);
- arraylistnew arraylist
- String []title={"Apple","Google","Facebook"};
- String []date={"2-12","5-16","9-12"};
- For (int i = 0; i < 3; i++)
- {
- hashmap<string, object> map = new hashmap<string, object> ();
- Map.put ("More_image", R.drawable.more); ID of the image resource
- Map.put ("title", Title[i]);
- Map.put ("date", Date[i]);
- Listitem.add (map);
- }
- Data source
- Simpleadapter listitemadapter= New Simpleadapter (selectoractivity. This, ListItem,
- R.layout.item,//ListItem XML Implementation
- //dynamic array with imageitem corresponding to the child
- New string[] { "more_image", "title", "date"},
- //XML file inside a ImageView, two x TextView ID
- new int[] {r.id.more_image, r.id.title,
- R.id.date});
- List.setadapter (Listitemadapter);
- }
- }
At last
Figure-1 Click on the background color
Fig. 2 Background color when tumbling
Android settings listview Item Check background color