Using Android to learn layout layouts

Source: Internet
Author: User

 Android Common layout

LinearLayout (Linear layout)   
The linear vertical level of the
Relativelaytout (relative layout)
The most flexible
Tablelayout (Table layout)
Use the GridView instead
Absolutelayout (Absolute layout)
It's best not to use absolute coordinates
Framelayout (Frame layout)
Use a ring scroll bar such as a video buffer when layout overlay
Frequency of Use
Absolute<table<frame<linear<relative
Android Layout principles
(1) Use linear layout and relative layout as much as possible do not use absolute layout
(2) In the same layout hierarchy, it is recommended to use linear layout instead of relative layout because the linear layout performance is slightly higher.
(3) Extracting reusable components and using the Include tag
(4) Use the viewstub tag to load some infrequently used layouts (temporarily unused)
(5) Reduce the nesting level of tags with the merge tag
Use of <include/>
Role: Extract the common components out of a single XML file, and then use the Include tag to import the public layout
Effect: Improve UI production and reuse efficiency
View can also be found through Findviewbyid because the include is actually the direct inclusion of the self-layout into the public layout

1, sub-layout title.xml
<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"    Android:layout_ Width= "Match_parent"  android:layout_height= "30DP"    android: Background= "#243821"></linearlayout>

2, main layout main.xml

< LinearLayout     Android:layout_height = "Match_parent"     xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width  = "Match_parent"    android:orientation= "vertical">    <  layout= "@layout/title"        ></include>
</LinearLayout>
Merge UI layouts with merge
What it does: Merge UI layouts that reduce the nesting level of the UI layout
Scene 1; The layout root node is framelayout and does not need to set properties such as Backgroud and padding, you can use merge instead
Scenario 2: When a layout is used as a self-layout by another layout include, use merge as the top point of the layout, which is then introduced,
The top contact is automatically ignored
1, sub-layout progress.xml
<?XML version= "1.0" encoding= "Utf-8"?><Mergexmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ProgressBarAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/progress"android:layout_gravity= "Center"/></Merge>

2, main layout main.xml

<LinearLayoutAndroid:layout_height= "Match_parent"xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"android:orientation= "vertical">    <includeLayout= "@layout/title"        ></include>    <FramelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content">        <includeLayout= "@layout/progress"/>    </Framelayout>
</LinearLayout>
Lazy Loading with viewstub
Role: The viewstub tag can be used as a incude tag to introduce an external layout, unlike the viewstub introduced by default does not expand the layout,
Neither occupy the display nor occupy the location, thus saving CPU and memory when parsing the Laytou
1, sub-layout common.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content">    <TextViewAndroid:id= "@+id/text"Android:text= "Viewstub"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>

2, main layout main.xml

<LinearLayoutAndroid:layout_height= "Match_parent"xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"android:orientation= "vertical">    <includeLayout= "@layout/title"        ></include>    <FramelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content">        <includeLayout= "@layout/progress"/>    </Framelayout>    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/btn"Android:text= "button"        />    <viewstubAndroid:id= "@+id/viewstub"Android:layout= "@layout/common"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>

3,mainactivity

 Public classMainactivityextendsActivity {PrivateButton btn; Privateviewstub viewstub; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.ID.BTN); Viewstub=(viewstub) Findviewbyid (r.id.viewstub); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {viewstub.inflate ();    }        }); }}

  

Using Android to learn layout layouts

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.