zoukankan      html  css  js  c++  java
  • (转) Android中ViewStub组件使用

    1. 概述:

        ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化。它们之间最大的差别 是,ViewStub中的布局不会随着它所在布局的渲染而渲染,而<include>标签中的布局会随着它所在布局的渲染而渲 染,ViewStub中的布局只有在你需要的时候才会渲染到主界面中。

    2. 效果图:

       (1)在ButtonOne与ButtonTwo之间存在一个ViewStub布局,如下图:

     ViewStub1

       (2)单击ButtonOne后渲染ViewStub中的布局,如下图:

    ViewStub2

    3. 实现代码:

        (1)res/layout/main.xml实现:

    [java:firstline[1]] view plaincopy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2.   
    3. <LinearLayout 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.     >  
    8.       
    9.     <Button  
    10.         android:id = "@+id/show"  
    11.         android:text = "ButtonOne"  
    12.         android:layout_width = "wrap_content"  
    13.         android:layout_height = "wrap_content"  
    14.         />  
    15.       
    16.     <ViewStub  
    17.         android:id = "@+id/viewStub"  
    18.         android:layout = "@layout/green_layout"  
    19.         android:layout_width = "300dip"  
    20.         android:layout_height = "300dip"  
    21.         />  
    22.           
    23.     <Button  
    24.         android:layout_width = "wrap_content"  
    25.         android:layout_height = "wrap_content"  
    26.         android:text = "ButtonTwo"  
    27.         />  
    28.       
    29. </LinearLayout>  

        (2)main.xml中ViewStub组件里的布局实现:

    [java:firstline[1]] view plaincopy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2.   
    3. <LinearLayout  
    4.     xmlns:android = "http://schemas.android.com/apk/res/android"  
    5.     android:layout_width = "match_parent"  
    6.     android:layout_height = "match_parent"  
    7.     android:background = "@color/green">  
    8.       
    9. </LinearLayout>  

        (4)主Activity实现:

    [java:firstline[1]] view plaincopy
    1. package com.focus.fishme;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.view.View;  
    6. import android.view.ViewStub;  
    7. import android.view.View.OnClickListener;  
    8. import android.widget.Button;  
    9.   
    10. public class ViewStubActivity extends Activity {  
    11.       
    12.     private ViewStub mViewStub;  
    13.       
    14.     private Button mShow;  
    15.       
    16.     @Override  
    17.     public void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.main);  
    20.   
    21.         mViewStub = (ViewStub) findViewById(R.id.viewStub);  
    22.           
    23.         mShow = (Button) findViewById(R.id.show);  
    24.         mShow.setOnClickListener(new OnClickListener() {  
    25.             public void onClick(View view) {  
    26.                 if (mViewStub != null) {  
    27.                     mViewStub.inflate();  
    28.                 }  
    29.             }  
    30.         });  
    31.     }  
    32.       
    33. }  
  • 相关阅读:
    JAVASCRIPT首页广告轮流显示
    ASP.NET (C#) 面试笔试题目收集
    ASP 按修改时间读取文件夹中文件并且排序
    CSS Body宽度和高度
    ASP.NET页面间的传值的几种方法(总结)
    IFRAME高度自适应——框架高度等于其页面高度
    ActionScript 3.0 Step By Step系列(八):动态的数据展现源于灵活的数据绑定
    ActionScript 3.0 Step By Step系列(五):走在面向对象开发的路上,以类为基础去思考编程问题
    ActionScript 3.0 Step By Step系列(四):来自面向对象开发之前的呐喊:“学会写可重用的代码”
    ActionScript 3.0 Step By Step系列(九):使用样式(style)和皮肤(Skin)两大画笔为应用程序界面画妆
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2652000.html
Copyright © 2011-2022 走看看