Android Layout View initialization code is automatically generated, androidview
In the process of android development, the interface layout is important, but also complicated. Sometimes we are eager to run and view the layout effect. However, I really don't want to talk about the compilation speed of android, especially when the layout is getting more and more complex, the project is getting bigger and bigger, and the resource files are getting more and more.
In particular, the findViewbyId is completely physical, and we can automatically generate the initialization code of the view based on the layout file.
First, declare:
1. This is easy to implement and has a general practicability, but it is easy to use in Complex layout and the first write initialization of View code.
2. Only the initialization code of the view with id labels can be generated.
Ideas
In fact, it is very easy to parse the layout file, save some information (tag type, id name, etc.) of tags with id attributes, and then generate fixed code based on the information.
Implementation
Directly add the code. First, parse the layout file and put the parsed 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(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;}}
Then splice the Code a little bit.
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 blocke. printStackTrace (); return null;} List <IdNamePair> res = mySax. getRes (); StringBuilder sb = new StringBuilder (); StringBuilder sb1 = new StringBuilder (); sb. append ("// ---------- start to define the 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 main method of the test class is used.
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]));}}}
Can the Android Code directly add system components to the current View without the xml layout file?
Yes. If you want to add a View, you can first obtain the layout, and then add view. addView () to the layout ();
A simple example of android code Layout
(Reference the example above)
// Initialize linear Layout
LinearLayout linearLayout = new LinearLayout (context );
// Set the direction of the linear layout to the vertical direction
LinearLayout. setOrientation (LinearLayout. VERTICAL );
// Set the filling mode of the linear layout to adaptive
LinearLayout. setLayoutParams (new LayoutParams (
LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ));
// Set the alignment in the linear layout to the horizontal center of the control
LinearLayout. setGravity (Gravity. CENTER_HORIZONTAL );
// Initialize the image button
ImageButton news = new ImageButton (context );
// Set the image button id
News. setId (85 );
// Set the listener for the image button
News. setOnClickListener (this );
// Set the image for the image button
News. setBackgroundResource (R. drawable. news );
// Set the attributes of the image button
News. setLayoutParams (new LayoutParams (33, 33 ));
// Add the image button to the linear Layout
LinearLayout. addView (news );
A simple small example is like the above. In fact, it is very simple. Just like you want to build a house with tables and benches in it, you need to set various properties of the table and bench, with length, width, and height, style or something, then set the position of the table bench, and finally put the table bench in that position according to your ideas.