Android abstract layout include merge Viewstub

Source: Internet
Author: User

Android abstract layout include merge Viewstub
1. layout Reuse

Labels can reuse layout files. Simple usage is as follows:

 

1) The tag can use a separate layout attribute, which is also required.

2) Other attributes can be used. If the tag specifies the ID attribute and your layout defines the ID, your layout ID will be overwritten. solution.

3) All android: layout _ * in the include tag are valid, provided that the layout_width and layout_height attributes must be written.

4) The layout can contain two identical include tags. You can use the following method for reference (refer ):

 

View bookmarks_container_1= findViewById(R.id.bookmarks_favourite); bookmarks_container_1.findViewById(R.id.bookmarks_list);View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite); bookmarks_container_2.findViewById(R.id.bookmarks_list);
2. Reduce view levels

Tags play an important role in UI structure optimization. They can delete redundant levels and optimize the UI. Mostly used to replace FrameLayout or when one layout contains another, Labels remove unnecessary view groups in the view hierarchy. For example, if your main layout file is a vertical layout and a vertical layout include is introduced, it makes no sense if the include layout uses LinearLayout, But it slows down your UI performance. You can use Label optimization.



Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/add"/>

Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/delete"/>



Now, when you add the layout file (Use Label), the system ignores And add two buttons directly. More For more information, see Android Layout Tricks #3: Optimize by merging.

 

3. Use it as needed

The biggest advantage of a tag is that it is loaded only when you need it. Using it does not affect the UI initialization performance. You can use a variety of infrequently used la S such as progress bars and error messages. Labels to reduce memory usage and accelerate rendering. Is an invisible View with a size of 0. The label is as follows:

Android: id = "@ + id/stub_import"
Android: inflatedId = "@ + id/panel_import"
Android: layout = "@ layout/progress_overlay"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "bottom"/>

When you want to load the layout, you can use one of the following methods:

(ViewStub) findViewById (R. id. stub_import). setVisibility (View. VISIBLE );
// Or
View importPanel = (ViewStub) findViewById (R. id. stub_import). inflate ();

When the inflate () function is called, ViewStub is replaced by the referenced resource and the referenced view is returned. In this way, the program can directly obtain the referenced view without calling the findViewById () function again.
Note: ViewStub does not support ViewStub yet. Label.

Android: id = "@ + id/recipients_editor_stub"
Android: layout_width = "match_parent"
Android: layout_height = "45dip"
Android: layout = "@ layout/recipients_editor"/>

ViewStub stub = (ViewStub) findViewById (R. id. recipients_editor_stub );
If (stub! = Null ){
View stubView = stub. inflate ();
MRecipientsEditor = (RecipientsEditor) stubView. findViewById (R. id. recipients_editor );
MRecipientsPicker = (ImageButton) stubView. findViewById (R. id. recipients_picker );
}

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.