Practical Android skills: viewstub applications

Source: Internet
Author: User

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 view. Of course, you 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:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:gravity="center_horizontal">  <ViewStub     android:id="@+id/viewstub_demo_text"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="5dip"    android:layout_marginRight="5dip"    android:layout_marginTop="10dip"    android:layout="@layout/viewstub_demo_text_layout"/>  <ViewStub     android:id="@+id/viewstub_demo_image"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="5dip"    android:layout_marginRight="5dip"    android:layout="@layout/viewstub_demo_image_layout"/></LinearLayout>

For textview layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="wrap_content"  android:layout_height="wrap_content">    <TextView        android:id="@+id/viewstub_demo_textview"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#aa664411"        android:textSize="16sp"/></LinearLayout>

For the imageview layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="wrap_content"  android:layout_height="wrap_content">    <ImageView        android:id="@+id/viewstub_demo_imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></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:

package com.effective;import android.app.Activity;import android.os.Bundle;import android.view.ViewStub;import android.widget.ImageView;import android.widget.TextView;public class ViewStubDemoActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.viewstub_demo_activity);        if ((((int) (Math.random() * 100)) & 0x01) == 0) {            // to show text            // all you have to do is inflate the ViewStub for textview            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);            stub.inflate();            TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);            text.setText("The tree of liberty must be refreshed from time to time" +                    " with the blood of patroits and tyrants! Freedom is nothing but " +                    "a chance to be better!");        } else {            // to show image            // all you have to do is inflate the ViewStub for imageview            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);            stub.inflate();            ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);            image.setImageResource(R.drawable.happy_running_dog);        }    }}

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.

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.