Android principle-dynamic code layout and android principle Layout

Source: Internet
Author: User

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
  • Controls cannot be reused

    // Add for the first time
    MLinearLayout. addView (mTextView, mLayoutParams );
    // Add for the second time
    MLinearLayout. addView (mTextView, mLayoutParams );
    // We added the mTextView twice. This is not allowed. In the parent class layout, there can only be unique objects and cannot be repeated.

  • Whether the IDs of different activities are the same

    Set ID directlysetId(1)No.
    ID generation must useView.generateViewId()
    My opinion is to create a static tool class to generate an ID
    Whether an error occurs when the int value of the ID is the same.

  • Some common code

    TextView. setTextColor (0xffff0000 );
    Layout. setBackgroundColor (0x00000000 );
    SetOrientation (LinearLayout. VERTICAL );
    SetGravity (Gravity. CENTER_VERTICAL)
    SetPadding (10, 5, 5, 5 );
    SetMargin (8, 0, 0, 0 );
    Lp. gravity = Gravity. CENTER_VERTICAL;
    Lp. topMargin = 5;
    Lp. addRule (RelativeLayout. CENTER_VERTICAL );
    Lp. addRule (RelativeLayout. RIGHT_OF, ID_IMAGE_HEAD );

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.

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.