The Method of Custom ListView Background Beautified by Android Program

  • 2021-07-26 08:57:44
  • OfStack

In this paper, an example is given to describe the method of beautifying the custom ListView background of Android program. Share it for your reference, as follows:

In Android, ListView is one of the most commonly used controls, When doing UI design, Many people want to change its background. To make him conform to the overall UI design, it is very simple to change the background back. Just prepare a picture and specify the attribute android: background= "@ drawable/bg", but don't be too happy. When you do this, you find that the background has changed, but when you drag or click on the blank position of list, you find that ListItem has turned black, which destroys the overall effect.

Why is this?

This should start with the effect of Listview. The default ListItem background is transparent, The background of ListView is fixed, Therefore, in the process of scrolling the scroll bar, if the display content of each Item is mixed with the background in real time, the android system uses an attribute called android: cacheColorHint in order to optimize this process, and the default color value under the black theme is # 191919, so the picture just now appears, and one and a half of them are black.

What can I do?

If you just change the background color, you can directly specify android: cacheColorHint as your desired color. If you use pictures as the background, you only need to specify android: cacheColorHint as transparent (# 00000000). Of course, in order to beautify, you have to sacrifice 1 efficiency. In the end, the results you don't want will not appear!

Customize the dividing line between ListView lines

In the Android platform, the system controls provide flexible custom options. All widget controls based on ListView or AbsListView can set the dividing line of line spacing by the following methods, and the dividing line can customize colors or pictures.

In ListView, we use the attribute android: divider= "# FF0000" to define the separator as red. Of course, the value here can point to one drawable picture object. If the picture is used, the height may be greater than the default pixel of the system. You can set the height by yourself, such as 6 pixels android: dividerHeight= "6px". Android development network prompt of course, ListView in Java.

1) There is no background color change when clicking Item

Add the following properties to the ListView control in the xml file:


android:listSelector="@drawable/timer_list_selector"

Define the attribute value of timer_list_selector in drawable

As defined in timer_list_selector. xml:


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true" android:drawable="@android:color/transparent" />
</selector>

transparent is defined in colors. xml under the values folder as follows:


<color name="transparent">#50000000</color>

2) Set no gap between Item

Add the following properties to the ListView control in the xml file:


android:divider="#00000000"

Or as defined in javaCode:


listView.setDividerHeight(0);

3) Calling the notifyDataSetChanged () method in a custom BaseAdapter recalls the getView () method of BaseAdapter.

Friends with heart should find that after setting the background in listview. There will be some problems.

1. When listview is dragged, the background picture disappears and turns into a black background. After dragging, our own background picture will not be displayed.

Problem 1 is solved by the following code knot


android:scrollingCache="false"

2. There are black shadows on the top and bottom of listview.

Problem 2 is solved with the following code:


android:fadingEdge="none"

3. One picture should be set as an interval between every item of lsitview.
Problem 3 is solved with the following code:


android:divider="@drawable/list_driver"

Where: @ drawable/list_driver is a picture resource

The overall situation is as follows


<ListView
 android:id="@+id/myListView01"
 android:layout_width="fill_parent"
 android:layout_height="287dip"
 android:fadingEdge="none"
 android:divider="@drawable/list_driver"
 android:scrollingCache="false"
 android:background="@drawable/list">
</ListView>

More readers interested in Android can check the topics of this site: "Summary of View Skills in Android View", "Summary of SD Card Operation Methods in Android Programming Development", "Introduction and Advanced Tutorial of Android Development", "Summary of Android Resource Operation Skills", "Summary of Android File Operation Skills" and "Summary of Android Control Usage"

I hope this paper is helpful to everyone's Android programming.


Related articles: