Android uses viewstub to complete dynamic loading in layout XML

@H_ 419_ 1 @ I. two common module loading methods for layout XML files

1. Static loading: the loading time of the loaded module is the same as that of other modules.    

2. Dynamic loading: the module to be loaded is not loaded into memory at the beginning. It will be loaded dynamically only when you need to load this module.    

Also add layout width and height

@H_ 419_ 1 @ II. Viewstup completes dynamic loading

1. Introduction

2. Viewstub class structure diagram

From this figure, we can find that viewstub is a control. If it's a control, it's easy to do. The operations that can be performed on other controls (such as initialization, creating objects, and using)

It can be carried out.

Viewstub is a control, so the properties are all under Android, including layout

So we can use a button to control the dynamic loading of viewstub.

3. Viewstub attribute method

The inflate () method is used to load the viewstub.

The dynamic function can be realized by adding this inflate () in the onclick method of the button.

@H_ 419_ 1 @ III. detailed introduction to viewstub

When developing applications, we often encounter such situations. We will dynamically decide which view or layout to display according to the conditions at run time. The most common idea is to write all the views that may be used on it, first set their visibility to view. Go, and then dynamically change its visibility in the code. The advantage of this approach is simple logic and flexible control. But its disadvantage is that it consumes resources. Although the initial view of the view is visible to view.gone, the view will still be inflated during the inflate layout, that is, objects will still be created, instantiated and properties will be set. In other words, it will consume resources such as memory.

The recommended method is to use android.view.viewstub. Viewstub is a lightweight view. It is an invisible control that does not occupy the layout position and occupies very small resources. You can specify a layout for the viewstub. Only the viewstub will be initialized when the layout is interpolated. Then, when the viewstub is set to visible, or when viewstub. Inflate() is called, the layout to which the viewstub is directed will be interpolated and instantiated, and then the layout properties of the viewstub will be passed to the layout it points to. In this way, you can use viewstub to conveniently display a layout or not at run time.

But viewstub is not omnipotent. Here is a summary of what viewstub can do, when to use viewstub and when to use visibility control.

@H_ 419_ 1 @ first, let's talk about some features of viewstub:

1. Viewstub can only be inflated once, and then the viewstub object will be set to null. In other words, after a layout specified by viewstub is inflated, it will not be controlled by viewstub.

2. Viewstub can only be used to inflate a layout file, not a specific view. Of course, you can also write the view in a layout file.

Based on the above characteristics, viewstub can be used:

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

Because viewstub can only be inflated once and will be empty later, you can't expect to use viewstub to control the layout later. Therefore, when a layout needs to be displayed and hidden more than once at runtime, viewstub cannot do so. At this time, you can only use the visibility of view to control.

2. It is a layout file rather than a view that wants to control display and hiding.

Because the viewstub can only be set to the ID of a layout file, it cannot control a view.

Therefore, if you want to control the display and hiding of a view (such as button or textview), or you want to continuously display and hide a layout or view at runtime, you can only use the visibility of view to control it.

@H_ 419_ 1 @ let's take a look at an example

In this example, two different layouts are displayed. One is to display a text with textview, and the other is to display a picture with ImageView. The two are displayed when oncreate(). This is the best place to apply viewstub.

Let's take a look at the layout first. One is the main layout, in which only two viewstubs are defined, one is used to control textview, the other is used to control ImageView, the other is the textview layout for displaying text, and the other is the layout for ImageView:

Layout for textview:

Layout for ImageView:

Let's look at the code and decide which one to display. We only need to find the corresponding ViewStub and then call infalte () to get the corresponding layout.

Operation results:

@H_ 419_ 1 @ precautions for use:

1. Some layout attributes will only work if they are added to the viewstub instead of the actual layout, such as Android: layout used above_ The margin * series attributes will not work if they are added to the textview. They will only work if they are placed on its viewstub. The properties of viewstub will be passed to the corresponding layout after inflate().

@H_ 419_ 1 @ summary

The above is what Xiaobian introduced to you. Android uses viewstub to complete dynamic loading in layout XML. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>