Style settings of listview in Android

Source: Internet
Author: User

In Android, listview is the most commonly used control. During the uidesign, many people want to change its background so that it can conform to the overall uidesign, it's easy to change the background. You only need to prepare an image and specify the property Android: Background = "@ drawable/BG". But don't be so happy. When you do this, you will find that the background has changed, however, when you drag or click the blank position of the list, you can find that all listitems are black, which damages the overall effect.

Why?

Starting from the effect of listview, the default listitem background is transparent, while the background of listview is fixed, therefore, if the display content of each item is mixed with the background in real time during scroll of the scroll bar, the android system uses android to optimize this process: the default color value of cachecolorhint in the black topic is #191919. Therefore, half of the image is black.

What should we do?

If you only change the background color, you can directly specify the Android: cachecolorhint as the color you want. If you use an image as the background, you only need to change the Android: cachecolorhint can be specified as transparent (#00000000). Of course, to beautify the image, you must sacrifice some efficiency. In the end, we won't see what you didn't want!
Custom split lines of listview

The system control on the Android platform provides flexible custom options. All widgets implemented based on listview or abslistview can set the line spacing through the following methods, the split line can be customized with colors or images.

In listview, we use the property Android: divider = "# ff0000" to define the separator as red. Of course, the value here can point to a drawable image object, if the image height is greater than the system's default pixel, you can set the height by yourself, for example, 6 pixels Android: dividerheight = "6px ", android Development Network prompts that, of course, listview can also be set in Java.

 

1) No background color changes when you click item
Add the following attributes to the listview control in the XML file:
Android: listselector = "@ drawable/timer_list_selector"
Define the attribute value of timer_list_selector in drawable.
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 defined in colors. XML in the values folder is as follows:
<Color name = "Transparent" >#50000000 </color>

2) set no gap between items
Add the following attributes to the listview control in the XML file:
Android: divider = "#00000000"
Or define it in javacode:
Listview. setdividerheight (0 );

3) Calling the notifydatasetchanged () method in the Custom baseadapter will re-call the getview () method of the baseadapter.

 

Attribute name

description

Android: choicemode

ThisListviewThe selected mode. By default,ListNo mode is selected.

The property value must be set to one of the following constants:None, Value:0, Indicating no selection mode;

Singlechoice, Value:1, Indicates that up to one item can be selected;

Multiplechoice, Value:2, Indicates that multiple items can be selected.

See Global attribute resource symbols.Choicemode.

Android: divider

RulesListProjects are separated by a certain image or color. Available"@ [+] [Package:] type: Name"Or"? [Package:] [type:] Name"(Topic attribute) to point to an existing resource. You can also use"# RGB","# Argb","# Rrggbb"Or"# Aarrggbb"To indicate a color.

See Global attribute resource symbols.Divider.

Android: dividerheight

The height of the separator. If the height is not specified, the inherent height of this separator is used. Must be a floating point with a unit, such"14.5sp". Available units suchPx(PixelPixels ),DP(Density-independent pixelsPixels unrelated to the density), SP(Scaled pixels Based on preferred font sizeFixed proportion of pixels Based on font size ),In (inchesInch), Mm (millimetersMillimeters).

Available"@ [Package:] type: Name

"Or"? [Package:] [type:] Name"(Topic attribute) format to point to a resource that contains this type of value.

See Global attribute resource symbols.Dividerheight.

Android: entries

ReferenceListview. If the array is fixedProgramIs easier to write.

Must start"@ [+] [Package:] type: Name"Or"? [Package:] [type:] Name"To point to a resource.

See Global attribute resource symbols.Entries.

Android: footerdividersenabled

SetFlaseThisListviewNo separator will be drawn before the footer view. The default value of this attribute isTrue.

Attribute value must be setTrueOrFalse.

Available"@ [Package:] type: Name

"Or"? [Package:] [type:] Name"(Topic attribute) format to point to a resource that contains this type of value.

See Global attribute resource symbols.Footerdividersenabled.

Android: headerdividersenabled

SetFlaseThisListviewNo separator will be drawn after the header view. The default value of this attribute isTrue.

Attribute value must be setTrueOrFalse.

Available"@ [Package:] type: Name

"Or"? [Package:] [type:] Name"(Topic attribute) format to point to a resource that contains this type of value.

See Global attribute resource symbols.Headerdividersenabled.

Attentive friends should find that after the background is set in listview. There will be some problems.

1. When the listview is dragged, the background image disappears into a black background. After the drag is completed, our own background image will be displayed.

2. There is a black shadow on the top and bottom of the listview.

3. Set an image as the interval between each item in lsitview.

Set the statement in the XML file of listview to solve the above problem.

Question 1:CodeAndroid: scrollingcache = "false"

Problem 2: Use the following code to solve the problem: Android: fadingedge = "NONE"
Problem 3: Android: divider = "@ drawable/list_driver", where @ drawable/list_driver is an image resource.

 

Overall:

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

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.