ViewStub用法介绍

ViewStub用法介绍在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局。那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在代码中动态的更改它的可见性。这样的做法的优点是逻辑简单而且控制起来比较灵活。但是它的缺点就是,耗费资源。虽然把View的初始可见View.GONE但是在Inflate布局的时候View仍然会被Infl

大家好,又见面了,我是你们的朋友全栈君。在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局。那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在代码中动态的更改它的可见性。这样的做法的优点是逻辑简单而且控制起来比较灵活。但是它的缺点就是,耗费资源。虽然把View的初始可见
View.GONE但是在Inflate布局的时候View仍然会被Inflate,也就是说仍然会创建对象,会被实例化,会被设置属性。也就是说,会耗费内存等资源。

      推荐的做法是使用android.view.ViewStub,ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate和实例化,然后ViewStub的布局属性都会传给它所指向的布局。这样,就可以使用ViewStub来方便的在运行时,要还是不要显示某个布局。

      但ViewStub也不是万能的,下面总结下ViewStub能做的事儿和什么时候该用ViewStub,什么时候该用可见性的控制。

     首先来说说ViewStub的一些特点:

         1. ViewStub只能Inflate一次,之后ViewStub对象会被置为空。按句话说,某个被ViewStub指定的布局被Inflate后,就不会够再通过ViewStub来控制它了。

         2. ViewStub只能用来Inflate一个布局文件,而不是某个具体的View,当然也可以把View写在某个布局文件中。

     基于以上的特点,那么可以考虑使用ViewStub的情况有:

         1. 在程序的运行期间,某个布局在Inflate后,就不会有变化,除非重新启动。

              因为ViewStub只能Inflate一次,之后会被置空,所以无法指望后面接着使用ViewStub来控制布局。所以当需要在运行时不止一次的显示和隐藏某个布局,那么ViewStub是做不到的。这时就只能使用View的可见性来控制了。

         2. 想要控制显示与隐藏的是一个布局文件,而非某个View。

              因为设置给ViewStub的只能是某个布局文件的Id,所以无法让它来控制某个View。

     所以,如果想要控制某个View(如Button或TextView)的显示与隐藏,或者想要在运行时不断的显示与隐藏某个布局或View,只能使用View的可见性来控制。

下面来看一个实例

在这个例子中,要显示二种不同的布局,一个是用TextView显示一段文字,另一个则是用ImageView显示一个图片。这二个是在onCreate()时决定是显示哪一个,这里就是应用ViewStub的最佳地点。

先来看看布局,一个是主布局,里面只定义二个ViewStub,一个用来控制TextView一个用来控制ImageView,另外就是一个是为显示文字的做的TextView布局,一个是为ImageView而做的布局:

  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>  
<?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>

为TextView的布局:

  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>  
<?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>

为ImageView的布局:

  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>  
<?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>

下面来看代码,决定来显示哪一个,只需要找到相应的ViewStub然后调用其infalte()就可以获得相应想要的布局:

  1. package com.effective;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.ViewStub;  
  6. import android.widget.ImageView;  
  7. import android.widget.TextView;  
  8.   
  9. public class ViewStubDemoActivity extends Activity {  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.viewstub_demo_activity);  
  14.         if ((((int) (Math.random() * 100)) & 0x01) == 0) {  
  15.             // to show text   
  16.             // all you have to do is inflate the ViewStub for textview   
  17.             ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);  
  18.             stub.inflate();  
  19.             TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);  
  20.             text.setText(“The tree of liberty must be refreshed from time to time” +  
  21.                     ” with the blood of patroits and tyrants! Freedom is nothing but “ +  
  22.                     “a chance to be better!”);  
  23.         } else {  
  24.             // to show image   
  25.             // all you have to do is inflate the ViewStub for imageview   
  26.             ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);  
  27.             stub.inflate();  
  28.             ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);  
  29.             image.setImageResource(R.drawable.happy_running_dog);  
  30.         }  
  31.     }  
  32. }  
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);
        }
    }
}

运行结果:

ViewStub用法介绍ViewStub用法介绍

使用的时候的注意事项:

1. 某些布局属性要加在ViewStub而不是实际的布局上面,才会起作用,比如上面用的android:layout_margin*系列属性,如果加在TextView上面,则不会起作用,需要放在它的ViewStub上面才会起作用。而ViewStub的属性在inflate()后会都传给相应的布局。

文章来自:

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153982.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Nginx使用

    Nginx使用

    2021年7月10日
    69
  • attempted to return null from_unable to process jar entry

    attempted to return null from_unable to process jar entry**Springboot启动时报错Ifyouwantanembeddeddatabase(H2,HSQLorDerby),pleaseputitontheclasspath.**产生这个错误的原因是springboot的自动配置,如果你没有配置DataSource就会导致下图这个错误解决方案1@SpringBootApplication(exclude…

    2022年9月23日
    0
  • django3.0异步_java定时任务框架选型

    django3.0异步_java定时任务框架选型celery介绍Celery是由Python开发、简单、灵活、可靠的分布式任务队列,是一个处理异步任务的框架,其本质是生产者消费者模型,生产者发送任务到消息队列,消费者负责处理任务。Celery侧重

    2022年7月29日
    5
  • 通达信资金净流入公式_通达信主力资金净流入指标

    V1:=(C*2+H+L)/4*10;V2:=EMA(V1,13)-EMA(V1,34);V3:=EMA(V2,5);V4:=2*(V2-V3)*5.5;庄家秘密撤:IF(V4<=0,V4,0),COLOR00FF00,LINETHICK2;庄家秘密进:IF(V4>=0,V4,0),COLORFF00FF,LINETHICK2;V5:=(HHV(INDEXH,8)-INDEXC)/…

    2022年4月5日
    325
  • 数据结构和算法_数据库原理考试题库

    数据结构和算法_数据库原理考试题库前言2016年又是一个全新的开始,每到一年的这个时候,总是颇有感慨。想对过去的一年做一些总结,但又觉得经历和精力总是不够。俗话说,一年之计在于春,当然,新的一年,也总是计划着N多事情,想做什么事情

    2022年8月1日
    1
  • android游戏开发引擎_android主题引擎

    android游戏开发引擎_android主题引擎随着Android系统的使用越来越广泛,了解一下Android平台下的游戏引擎就非常有必要。而同时因为基于Intelx86的移动设备越来越多,我也非常关注支持x86的移动游戏引擎。然而就目前为止游戏引擎的数量已经非常之多,每个引擎都有不同的特征、价格、成熟度等。通过一些调研之后,我发现有非常多的游戏引擎可用于开发运行在android移动设备端的游戏,其中有些还支持x86系统,另外还有些通过简单的

    2022年10月22日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号