In the process of Android development, interface layout is extremely important, but it is also complex. Sometimes we are eager to actually run to check the layout effect. But I really don't want to complain about the compilation speed of Android, especially when the layout is getting more and more complex, the projects are getting bigger and bigger, and the resource files are getting more and more.
Especially the initialization of android view, findViewbyId is completely physical. We can automatically generate the initialization code of the View based on the layout file.
First declare:
1. This is very easy to do, and it is average in practicality, but it is very useful when writing initialized View code for the first time.
2. Only initialization code for view with id tags can be generated.
Ideas
In fact, it is very simple. It is to parse the layout file, store some information about the tag with id attribute (label type, id name, etc.), and then generate fixed code based on this information.
accomplish
Directly uploading the code, first of all, parsing the layout file, putting the parsed information in a list
public class SaxHander extends DefaultHandler {
private List<IdNamePair> map = new ArrayList<IdNamePair>();
@Override
public void startDocument() throws SAXException {
();
();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
(uri, localName, qName, attributes);
// ("-------------------------------------");
String tempid = ("id");
String id = null;
if (tempid != null) {
String[] ss = ("/");
if (ss != null && == 2) {
id = ss[1];
}
}
if (id != null) {
(new IdNamePair(id, qName));
}
// (id);
// (qName);
}
public List<IdNamePair> getRes() {
return map;
}
}
public class IdNamePair {
private String id;
private String name;
/**
* @param id
* @param name
*/
public IdNamePair(String id, String name) {
super();
= id;
= name;
}
public String getId() {
return id;
}
public void setId(String id) {
= id;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
}
Then a little bit of splicing code
public class ViewCodeUtil {
static SAXParserFactory saxfac = ();
static SaxHander mySax = new SaxHander();
public static String getCode(String resFileName){
File f = new File(resFileName);
if (!()) {
return null;
}
try {
().parse(f,mySax);
} catch (Exception e) {
// TODO Auto-generated catch block
();
return null;
}
List<IdNamePair> res = ();
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
("//---------------------------------------------------------------------------------------------------------------------------
("//---------------------------------------------------------------------------------------------------------------------------
("public void initView() { \n");
for (IdNamePair idNamePair : res) {
(" private "+()+" "+ ()+()+";\n");
(" "+()+()+" = ("+()+")findViewById(."+()+");\n");
}
("}\n");
// (());
// (());
return (()).toString();
}
Finally, test the main method of the class.
public class Test {
private static final String[] layoutFiles ={"./res/g_ruler.xml","./res/"};
public static void main(String[] args) {
if (args!=null) {
for (int i = 0; i < ; i++) {
("");
("---------"+args[i]+"----------");
((args[i]));
}
}
for (int i = 0; i < ; i++) {
("");
("//---------"+layoutFiles[i]+"----------");
((layoutFiles[i]));
}
}
}