Dynamic skinning from the XML in the SD card get colorstatelist

Source: Internet
Author: User

Colorstatelist value = new Colorstatelist (states, colors);
Seeing Colorstatelist's construction method, we know that to get a colorstatelist, we need to have a
Int[][]

and a storage colorres.

Int[]

First look at a very common selector structure of the Color.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector    xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:color=" #ffffff "android:state_enabled=" true "android:state_pressed=" true "/>    <item android:color= "#000000"/></selector>
To parse such an XML document, use XML parsing library, I use Jsoup, in Androidstudio gradle add
Compile ' org.jsoup:jsoup:1.8.2 '
You can use this library.

Final file Colordir = new file (skindirectory, "color");//Specify the Color folder under the SD card directory, this directory is passed in by external if (Colordir.exists ())                    {Final file[] files = colordir.listfiles (Mxmlfilter);                        for (file file:files) {final Document parse = jsoup.parse (file, "UTF-8");//parse XML file into Document object                        Final Elements selectors = Parse.getelementsbytag ("selector");//Get selector node list, actually only one selector in the list                        if (Selectors.isempty ()) {continue;                        } final Element selector = selectors.get (0);                        Final Elements children = Selector.children ();                        Int[][] states = new Int[children.size () [];                        int[] colors = new int[children.size ()];                            for (int i = 0; i < children.size (); i++) {Element child = Children.get (i); if ("Item ". Equals (Child.tagname ())) {//Get Item node final Attributes Attributes = Child.attributes ()                                ; The attribute set of the//item node String value = Attributes.get ("Android:color");                                Final int parsecolor = Color.parsecolor (value);                                States[i] = Getstatesarr (attributes);                            Colors[i] = Parsecolor; }} colorstatelist value = new Colorstatelist (states, colors);


The main thing in this code is to take the property of each item, for example, Android:color is a property of item, take the value of color, and convert it to Colorint by Color.parsecolor.


Private int[] Getstatesarr (Attributes Attributes) {list<integer> statelist = new arraylist<> ();        String attributepress = Attributes.get ("android:state_pressed"); if (!            Textutils.isempty (Attributepress)) {Boolean ispressed = Boolean.parseboolean (attributepress); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_pressed);        } String Attributefocus = Attributes.get ("android:state_focused"); if (!            Textutils.isempty (Attributefocus)) {Boolean ispressed = Boolean.parseboolean (Attributefocus); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_focused);        } String Attributeselect = Attributes.get ("android:state_selected"); if (!            Textutils.isempty (Attributeselect)) {Boolean ispressed = Boolean.parseboolean (Attributeselect); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_selected); } String attributeactive = attributes.geT ("android:state_active"); if (!            Textutils.isempty (Attributeactive)) {Boolean ispressed = Boolean.parseboolean (attributeactive); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_active);        } String attributecheckable = Attributes.get ("android:state_checkable"); if (!            Textutils.isempty (Attributecheckable)) {Boolean ispressed = Boolean.parseboolean (attributecheckable); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_checkable);        } String attributechecked = Attributes.get ("android:state_checked"); if (!            Textutils.isempty (attributechecked)) {Boolean ispressed = Boolean.parseboolean (attributechecked); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_checked);        } String attributeenable = Attributes.get ("android:state_enabled"); if (! Textutils.isempty (Attributeenable)) {Boolean ispressed = Boolean.parseboolean (AttributeenabLe); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_enabled);        } String Attributewindowfocus = Attributes.get ("android:state_window_focused"); if (!            Textutils.isempty (Attributewindowfocus)) {Boolean ispressed = Boolean.parseboolean (Attributewindowfocus); Statelist.add ((ispressed? 1:-1) * Android.        r.attr.state_window_focused);        } if (Statelist.isempty ()) {return new int[]{};            }else{int[] Statearr =new int[statelist.size ()];            for (int i=0;i<statelist.size (); i++) {statearr[i] =statelist.get (i);        } return Statearr; }    }
This is the int value corresponding to each state, such as android:state_pressed to true, to add Android. R.attr.state_pressed to the array, if False to add-android. r.attr.state_pressed, negative.




Dynamic skinning from the XML in the SD card get colorstatelist

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.