The method of customizing the ListView background of Android program _android

Source: Internet
Author: User

This article illustrates the method of customizing the ListView background of Android program beautification. Share to everyone for your reference, specific as follows:

In Android, ListView is the most commonly used control, when doing UI design, many people want to be able to change its background, so that he can meet the overall UI design, change the background is very simple to prepare a picture and then specify attributes android:background= "@drawable/bg", but do not be happy too early, when you do so, found that the background is changed, but when you drag, or click on the list of blank positions, ListItem have become black, destroying the overall effect.

What is this for?

This is from the ListView effect, the default ListItem background is transparent, and listview background is fixed, so in the process of scrolling scroll if the current each item display content with the background of the mixed operation, So in order to optimize this process, the Android system uses a property called Android:cachecolorhint, and the default color value in the black theme is #191919, so there's just the picture, half black.

What about it?

If you just change the color of the background, you can directly specify Android:cachecolorhint for the color you want, if you are using a picture to do the background, it also as long as the android:cachecolorhint specified as transparent (#00000000) on it , of course, in order to beautify is to sacrifice some efficiency. In the end there is no return to the above said you do not want the results!

Custom ListView lines between rows

System controls in the Android platform provide flexible customization options, and all widget controls based on ListView or Abslistview implementations can set the line spacing split line in the following ways, dividing lines to customize colors, or pictures.

In ListView we use the attribute android:divider= "#FF0000" to define the delimiter as red, of course the value can point to a drawable picture object, if the picture may be higher than the system default pixel, you can set the height of 6 pixels Android:dividerheight= "6px", Android Development Network hints of course in Java ListView There are also relevant methods can be set.

1) No background color changes when clicking item

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

Android:listselector= "@drawable/timer_list_selector"

To define a Timer_list_selector property value in Drawable

The timer_list_selector.xml is defined as follows:

<?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>

The transparent is defined in the Colors.xml under the Values folder as follows:

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

2 Set no gap between the item

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

Android:divider= "#00000000"

or the following definition in Javacode:

Listview.setdividerheight (0);

3 The Notifydatasetchanged () method called in the custom Baseadapter will recall the Baseadapter GetView () method.

The attentive friend should find that after setting the background in the ListView. There will be some problems.

1. ListView The background picture disappears into a black background when dragging. Wait until we drag our own background picture to show it.

Problem 1 has the following code knot resolution

Android:scrollingcache= "false"

2, the top and bottom of the ListView has a black shadow.

Problem 2 is resolved with the following code:

Android:fadingedge= "None"

3, Lsitview each item needs to set a picture to do for the interval.
Problem 3 is resolved with the following code:

android:divider= "@drawable/list_driver"

Where: @drawable/list_driver is a picture resource

Overall 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>

For more information on Android-related content readers can view the site topics: "Android View tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating skills Summary", A summary of Android file manipulation tips and a summary of the usage of Android controls

I hope this article will help you with the Android program.

Related Article

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.