Android principle-dynamic code layout and android principle Layout
Dynamic code layout how to add code Layout
For example --Simple LinearLayout
LinearLayout llayout = new LinearLayout (mContext); llayout. setOrientation (LinearLayout. VERTICAL); LinearLayout. layoutParams layoutParams = new LinearLayout. layoutParams (LinearLayout. layoutParams. MATCH_PARENT, LinearLayout. layoutParams. MATCH_PARENT); llayout. setLayoutParams (layoutParams); Button btn = new Button (mContext); btn. setText ("This is Button"); btn. setPadding (8, 8, 8, 8); btn. setLayoutParams (lp); llayout. addView (btn); // This is to set the layout setContentView (llayout) in onCreate () of the Activity; btn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Toast. makeText (mContext, "This is dynamic activity", Toast. LENGTH_LONG ). show ();}});
Another example --Complex layout RelativeLayout
Difficulty: handling the relative positional relationship of child Controls
// Parent control RelativeLayout myLayout = new RelativeLayout (this); myLayout. setBackgroundColor (Color. BLUE); // two child controls: Button myButton = new Button (this); EditText myEditText = new EditText (this); // key: generate the corresponding ID myButton. setId (generateViewId (); myEditText. setId (generateViewId (); // the position of the child control, RelativeLayout. layoutParams buttonParams = new RelativeLayout. layoutParams (RelativeLayout. layoutParams. WRAP_CONTENT, RelativeLayout. layoutParams. WRAP_CONTENT); buttonParams. addRule (RelativeLayout. CENTER_HORIZONTAL); buttonParams. addRule (RelativeLayout. CENTER_VERTICAL); RelativeLayout. layoutParams textParams = new RelativeLayout. layoutParams (RelativeLayout. layoutParams. WRAP_CONTENT, RelativeLayout. layoutParams. WRAP_CONTENT); textParams. addRule (RelativeLayout. CENTER_HORIZONTAL); textParams. setMargins (0, 0, 0, 80); // focus on textParams. addRule (RelativeLayout. ABOVE, myButton. getId (); // Add the layout myLayout. addView (myButton, buttonParams); myLayout. addView (myEditText, textParams); setContentView (myLayout );
The key point isgenerateViewId()
, Which can be specifically put into the tool class:
/*** An {@ code int} value that may be updated atomically. */private static final AtomicInteger sNextGeneratedId = new AtomicInteger (1);/*** dynamically generate View ID * api level 17 or above. generateViewId () to generate * api level 17 or lower, you need to manually generate */public static int generateViewId () {if (Build. VERSION. SDK_INT <Build. VERSION_CODES.JELLY_BEAN_MR1) {for (;) {final int result = sNextGeneratedId. get (); // aapt-generated IDs have the high byte nonzero; clamp to the range under that. int newValue = result + 1; if (newValue> 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. if (sNextGeneratedId. compareAndSet (result, newValue) {return result ;}} else {return View. generateViewId ();}}
Code layout considerations
Performance Comparison Between code layout and XML Layout
Test a simple example
The Code layout is as follows:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; LinearLayout llayout = new LinearLayout(mContext); llayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT ); llayout.setLayoutParams(layoutParams); TextView tv = new TextView(mContext); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(8, 8, 8, 8); tv.setLayoutParams(lp); tv.setText("This is TextView"); tv.setPadding(8, 8, 8, 8); llayout.addView(tv); Button btn = new Button(mContext); btn.setText("This is Button"); btn.setPadding(8, 8, 8, 8); btn.setLayoutParams(lp); setContentView(llayout); }
Mean of three measurements: (23 + 28 + 20)/3 = 23.67 ms
Use the same XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="This is TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8px" android:padding="8px"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is Button" android:padding="8px"/></LinearLayout>
Mean of three measurements: (20 + 26 + 24)/3 = 23.33 ms
Conclusion: although the sample size is single and the number of samples is small, the Code layout and XML layout have the same loading time and performance.
Insufficient: more complex interface, not tested yet.
Idea: Code layout is necessary. If you summarize the code layout as a template and a library and use generics and reflection to reuse and automate the interface, it will be easier to expand than XML.