An initialization code method for automatically generating layout view in Android _android

Source: Internet
Author: User

In the Android development process, the interface layout is important, but it is also complex. Sometimes we rush to actually run to see the layout effect. But I really don't want to puke on Android, especially as the layout gets more complex, projects get bigger and more resources are available.

In particular, the initialization of the Android view, Findviewbyid is completely physical, and we can automatically generate the initialization code for the view based on the layout file.

First declare:

1. This is easy to do and practical, but it works well with complex layouts and first-write initialization view code.
2. Initialization code can only be generated for view with ID tags.

Ideas

In fact, it is very simple to parse the layout layout file, put some information (tag type, id name, etc.) of the label with ID attribute, and then generate the fixed code based on the information.

Realize

Directly on the code, first of all, the layout file parsing, the resolution of the information placed in a list

Copy Code code as follows:

public class Saxhander extends DefaultHandler {
Private list<idnamepair> map = new arraylist<idnamepair> ();


@Override
public void Startdocument () throws Saxexception {
Super.startdocument ();
Map.clear ();
}

@Override
public 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 ("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;

}
}

Copy Code code as follows:

public class Idnamepair {
Private String ID;
private String name;

/**
* @param ID
* @param name
*/
Public Idnamepair (string 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 stitching code

Copy Code code as follows:

public class Viewcodeutil {
static SAXParserFactory SAXFAC = Saxparserfactory.newinstance ();

static Saxhander Mysax = new Saxhander ();


public static string GetCode (String resfilename) {

File F = new file (resfilename);
if (!f.exists ()) {
return null;
}

try {
Saxfac.newsaxparser (). Parse (F,mysax);
catch (Exception e) {
TODO auto-generated Catch block
E.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.

Copy Code code as follows:

public class Test {

private static final string[] Layoutfiles ={"./res/g_ruler.xml", "./res/report.xml"};

public static void 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));
}

}

}

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.