Android uses ViewStub for dynamic loading in layout xml, and androidviewstub

Source: Internet
Author: User

Android uses ViewStub for dynamic loading in layout xml, and androidviewstub
Android uses ViewStub to perform dynamic loading in layout xml. I. Two common loading methods for Layout XML files:

1. Static Loading: The loaded modules are loaded at the same time as other modules.
<Include layout = "@ layout/otherLayout"/>


2. Dynamic Loading: the module to be loaded is not loaded into the memory at the initial stage. It will be dynamically loaded only when you need to load this module.
<ViewStub android: layout = "@ layout/otherLayout"/>

Add the layout width and height

  

Ii. Dynamic Loading of ViewStup

1. Introduction

      

2. ViewStub class structure diagram

    

From this figure, we can find that ViewStub is a control. It is a control, so you can perform operations on other controls (such as initialization and object creation)

It can all be done.

ViewStub is a control, so its attributes are all in android, including layout.

Therefore, we can use a button to control the dynamic loading of ViewStub.

3. ViewStub attribute Method

    

The inflate () method is used to load ViewStub.

You can add this inflate () in The onClick method of the Button to implement dynamic functions ().

 

3. ViewStub

When developing an application, you will often encounter such a situation and dynamically decide which View or layout to display based on the conditions during running. The most common idea is to write all the views that may be used on it, and set their visibility to View first.GONEAnd dynamically change its visibility in the code. This method has the advantages of simple logic and flexible control. However, it consumes resources. Although the initial View is visible.GONEHowever, in the Inflate layout, the View is still Inflate, that is, the object will be created, instantiated, and set attributes. That is to say, it will consume memory and other resources.

We recommend that you useAndroid. view. ViewStubViewStub is a lightweight View, which is an invisible control that occupies a very small resource and does not occupy the layout. You can specify a layout for ViewStub. In the Inflate layout, only ViewStub will be initialized. Then, when ViewStub is set to visible, or ViewStub is called. when inflate () is used, the layout directed by ViewStub will be Inflate and instantiated, and the layout attribute of ViewStub will be transmitted to the layout pointed to by it. In this way, ViewStub can be used to conveniently display a layout during running.

However, ViewStub is not omnipotent. Next we will summarize what ViewStub can do and when to use ViewStub and when to use visibility control.

First, let's talk about some features of ViewStub:

1.ViewStub can only be Inflate once, and then the ViewStub object will be left blank. In other words, after a layout specified by ViewStub is Inflate, it cannot be controlled by ViewStub.

2.ViewStub can only be used to Inflate a layout file, rather than a specific ViewYou can also write the View in a layout file.

Based on the above features, you can consider using ViewStub:

1. during the running of the program, a layout after Inflate will not change unless it is restarted.

Because ViewStub can only be Inflate once and will be left blank later, you cannot expect ViewStub to control the layout later. Therefore, ViewStub cannot display or hide a layout more than once during running. In this case, you can only control the visibility of the View.

2. To control the display and hiding of a layout file, rather than a View.

Because ViewStub can only be set to the Id of a layout file, it cannot be used to control a View.

Therefore, if you want to control the display and hiding of a View (such as a Button or TextView), or want to continuously display and hide a layout or View during runtime, only View visibility can be used for control.

Let's look at an instance.

In this example, two different la s are to be displayed. One is to use TextView to display a piece of text, and the other is to use ImageView to display an image. When onCreate () is used to determine which one to display. This is the best place to apply ViewStub.

Let's take a look at the layout. One is the main layout, which defines only two ViewStub, one is used to control TextView, and the other is the TextView layout for displaying text, one is the layout for ImageView:

 

[Html]View plain copy print?
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout
  3. Xmlns: android = "http://schemas.android.com/apk/res/android"
  4. Android: orientation = "vertical"
  5. Android: layout_width = "fill_parent"
  6. Android: layout_height = "fill_parent"
  7. Android: gravity = "center_horizontal">
  8. <ViewStub
  9. Android: id = "@ + id/viewstub_demo_text"
  10. Android: layout_width = "wrap_content"
  11. Android: layout_height = "wrap_content"
  12. Android: layout_marginLeft = "5dip"
  13. Android: layout_marginRight = "5dip"
  14. Android: layout_marginTop = "10dip"
  15. Android: layout = "@ layout/viewstub_demo_text_layout"/>
  16. <ViewStub
  17. Android: id = "@ + id/viewstub_demo_image"
  18. Android: layout_width = "wrap_content"
  19. Android: layout_height = "wrap_content"
  20. Android: layout_marginLeft = "5dip"
  21. Android: layout_marginRight = "5dip"
  22. Android: layout = "@ layout/viewstub_demo_image_layout"/>
  23. </LinearLayout>

For TextView layout:

 

 

[Html]View plain copy print?
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout
  3. Xmlns: android = "http://schemas.android.com/apk/res/android"
  4. Android: orientation = "vertical"
  5. Android: layout_width = "wrap_content"
  6. Android: layout_height = "wrap_content">
  7. <TextView
  8. Android: id = "@ + id/viewstub_demo_textview"
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "wrap_content"
  11. Android: background = "# aa664411"
  12. Android: textSize = "16sp"/>
  13. </LinearLayout>

For the ImageView layout:

 

 

[Html]View plain copy print?
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout
  3. Xmlns: android = "http://schemas.android.com/apk/res/android"
  4. Android: orientation = "vertical"
  5. Android: layout_width = "wrap_content"
  6. Android: layout_height = "wrap_content">
  7. <ImageView
  8. Android: id = "@ + id/viewstub_demo_imageview"
  9. Android: layout_width = "wrap_content"
  10. Android: layout_height = "wrap_content"/>
  11. </LinearLayout>

Next let's look at the code and decide which one to display. You only need to find the corresponding ViewStub and then call its infalte () to get the desired layout:

 

 

[Java]View plain copy print?
  1. Package com. Valid tive;
  2. Import android. app. Activity;
  3. Import android. OS. Bundle;
  4. Import android. view. ViewStub;
  5. Import android. widget. ImageView;
  6. Import android. widget. TextView;
  7. Public class ViewStubDemoActivity extends Activity {
  8. @ Override
  9. Public void onCreate (Bundle savedInstanceState ){
  10. Super. onCreate (savedInstanceState );
  11. SetContentView (R. layout. viewstub_demo_activity );
  12. If (int) (Math. random () * 100) & 0x01) = 0 ){
  13. // To show text
  14. // All you have to do is inflate the ViewStub for textview
  15. ViewStub stub = (ViewStub) findViewById (R. id. viewstub_demo_text );
  16. Stub. inflate ();
  17. TextView text = (TextView) findViewById (R. id. viewstub_demo_textview );
  18. Text. setText ("The tree of liberty must be refreshed from time to time" +
  19. "With the blood of patroits and tyrants! Freedom is nothing but "+
  20. "A chance to be better! ");
  21. } Else {
  22. // To show image
  23. // All you have to do is inflate the ViewStub for imageview
  24. ViewStub stub = (ViewStub) findViewById (R. id. viewstub_demo_image );
  25. Stub. inflate ();
  26. ImageView image = (ImageView) findViewById (R. id. viewstub_demo_imageview );
  27. Image. setImageResource (R. drawable. happy_running_dog );
  28. }
  29. }
  30. }

Running result:

 

Note:

1.Some layout attributes work only when they are added to ViewStub instead of the actual layout. For example, the android: layout_margin * series attributes are used above. If they are added to TextView, they do not work, it takes effect only when it is placed on its ViewStub. The attributes of ViewStub are transmitted to the corresponding layout after inflate.

Reference: http://blog.csdn.net/hitlion2008/article/details/6737537/

  

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.