Android中ViewStub组件使用
2013-10-13 11:53
357 查看
1. 概述:
ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化。它们之间最大的差别是,ViewStub中的布局不会随着它所在布局的渲染而渲染,而<include>标签中的布局会随着它所在布局的渲染而渲染,ViewStub中的布局只有在你需要的时候才会渲染到主界面中。
2. 效果图:
(1)在ButtonOne与ButtonTwo之间存在一个ViewStub布局,如下图:
(2)单击ButtonOne后渲染ViewStub中的布局,如下图:
3. 实现代码:
(1)res/layout/main.xml实现:
[java] view
plaincopy
<?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" >
<Button
android:id = "@+id/show"
android:text = "ButtonOne"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
<ViewStub
android:id = "@+id/viewStub"
android:layout = "@layout/green_layout"
android:layout_width = "300dip"
android:layout_height = "300dip" />
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "ButtonTwo" />
</LinearLayout>
(2)main.xml中ViewStub组件里的布局实现:
[java] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@color/green" >
</LinearLayout>
(4)主Activity实现:
[java] view
plaincopy
package com.focus.fishme;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ViewStubActivity extends Activity {
private ViewStub mViewStub;
private Button mShow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewStub = (ViewStub) findViewById(R.id.viewStub);
mShow = (Button) findViewById(R.id.show);
mShow.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mViewStub != null) {
mViewStub.inflate();
}
}
});
}
}
ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化。它们之间最大的差别是,ViewStub中的布局不会随着它所在布局的渲染而渲染,而<include>标签中的布局会随着它所在布局的渲染而渲染,ViewStub中的布局只有在你需要的时候才会渲染到主界面中。
2. 效果图:
(1)在ButtonOne与ButtonTwo之间存在一个ViewStub布局,如下图:
(2)单击ButtonOne后渲染ViewStub中的布局,如下图:
3. 实现代码:
(1)res/layout/main.xml实现:
[java] view
plaincopy
<?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" >
<Button
android:id = "@+id/show"
android:text = "ButtonOne"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
<ViewStub
android:id = "@+id/viewStub"
android:layout = "@layout/green_layout"
android:layout_width = "300dip"
android:layout_height = "300dip" />
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "ButtonTwo" />
</LinearLayout>
(2)main.xml中ViewStub组件里的布局实现:
[java] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@color/green" >
</LinearLayout>
(4)主Activity实现:
[java] view
plaincopy
package com.focus.fishme;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ViewStubActivity extends Activity {
private ViewStub mViewStub;
private Button mShow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewStub = (ViewStub) findViewById(R.id.viewStub);
mShow = (Button) findViewById(R.id.show);
mShow.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mViewStub != null) {
mViewStub.inflate();
}
}
});
}
}
相关文章推荐
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- android USB如何修改VID具体实现
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- android 多线程技术应用
- Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
- Android数据类型之间相互转换系统介绍
- 通过Android trace文件分析死锁ANR实例过程
- Android实现左右滑动效果的方法详解
- Android 各国语言缩写及简称详细介绍
- Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
- android IPC之binder通信机制
- Android应用开发中模拟按下HOME键的效果(实现代码)
- Android 读写文件方法汇总
- Android如何禁止横屏竖屏的变换
- Android中设置只有程序第一次运行才显示的界面实现思路