In the Android development process, the interface layout is important, but it is also complex. Sometimes we are anxious to actually run the view layout effect. But Android's compile speed I really do not want to vomit trough what, especially in the layout more and more complex, the project is getting bigger, the resource file more and more situation.
Especially with Android view initialization, Findviewbyid is completely physical, and we can automatically generate the view initialization code based on the layout file.
First, declare:
1. This is easy to do and practical, but it works well in complex layouts and first-time initialization of view code.
2. Only the initialization code of the view with the ID tag can be generated.
Ideas
In fact, it is very simple to parse layout file, the id attribute of the tag information (tag type, id name, etc.) stored up, and then based on this information to generate a fixed code.
Realize
Directly on the code, the first is the layout file parsing, the parsed out of the information in a list
public class Saxhander extends DefaultHandler {private list<idnamepair> map = new arraylist<idnamepair> (); @ overridepublic void Startdocument () throws Saxexception {super.startdocument (); Map.clear ();} @Overridepublic void Startelement (String uri, String localname, String qName, Attributes Attributes) throws Saxexception { Super.startelement (URI, LocalName, qName, attributes);//system.out.println ("------------------------------------- "); String tempid = Attributes.getvalue ("Android:id"); String id = null;if (tempid! = null) {string[] ss = Tempid.split ("/"); if (ss! = null && ss.length = = 2) {id = ss[1 ];}} if (id! = NULL) {Map.add (new Idnamepair (ID, qName));} SYSTEM.OUT.PRINTLN (ID);//system.out.println (qName);} Public list<idnamepair> Getres () {return map;}}
public class Idnamepair {private string id;private string name;/** * @param ID * @param name */public Idnamepair (strin G ID, String name) {super (); this.id = Id;this.name = name;} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
And then a little bit of splicing code
public class Viewcodeutil {static SAXParserFactory SAXFAC = Saxparserfactory.newinstance (); static Saxhander Mysax = new Saxhander ();p ublic static string GetCode (String resfilename) {file F = new file (resfilename); F (!f.exists ()) {return null;} try {saxfac.newsaxparser (). Parse (f,mysax);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;} list<idnamepair> res = Mysax.getres (); StringBuilder sb = new StringBuilder (); StringBuilder sb1 = new StringBuilder () sb.append ("//----------start defining domain--------------\ n"); Sb1.append ("//---------- Start Initview method------------------\ n "), Sb1.append (" public void Initview () {\ n "); for (Idnamepair idnamepair:res) { Sb.append ("Private" +idnamepair.getname () + "" + Idnamepair.getid () +idnamepair.getname () + "; \ n"); Sb1.append ("" +idnamepair.getid () +idnamepair.getname () + "= (" +idnamepair.getname () + ") Findviewbyid (R.id.") +idnamepair.getid () + "); \ n"); Sb1.append ("}\n");//system.out.println (sb.tostring ());//system.out.println (SB1.ToString ()); return Sb.append (Sb1.tostring ()). ToString ();
Finally, the test class Main method.
public class Test {private static final string[] Layoutfiles ={"./res/g_ruler.xml", "./res/report.xml"}; public static Voi D main (string[] args) {if (args!=null) {for (int i = 0; i < args.length; i++) {System.out.println (""); System.out.println ("---------" +args[i]+ "----------"); System.out.println (Viewcodeutil.getcode (Args[i));}} for (int i = 0; i < layoutfiles.length; i++) {System.out.println (""); SYSTEM.OUT.PRINTLN ("//---------" +layoutfiles[i]+ "----------"); System.out.println (Viewcodeutil.getcode (Layoutfiles[i]);}}}
Android layout view initialization code Auto-generated