Android中使用ViewStub来提高UI的加载的性能
2012-01-05 21:31
603 查看
首先看下API中的ViewStub
根据的文档的说明,ViewStub是一种默认不可见的试图,它没有大小,所以不能被改变,也不能通过某些把viewstub添加到布局当中来,
不过我们可以使用inflate()来吧ViewStub中的试图增加进行,这样可以实现动态的添加试图,不必要每次在onCreate()的时候就加载布局,可以提高我们的性能。
Demo中的使用方法:
1:新建布局文件 设置<ViewStub>节点
2: 在Activity中进行按钮点击viewStub = (ViewStub) findViewById(R.id.mystub);
3: View view = viewStub.inflate();把ViewStub中的View增添进来
下面Demo源代码:
主Activity类:
布局文件:
main.xml:
效果截图:
根据的文档的说明,ViewStub是一种默认不可见的试图,它没有大小,所以不能被改变,也不能通过某些把viewstub添加到布局当中来,
不过我们可以使用inflate()来吧ViewStub中的试图增加进行,这样可以实现动态的添加试图,不必要每次在onCreate()的时候就加载布局,可以提高我们的性能。
Demo中的使用方法:
1:新建布局文件 设置<ViewStub>节点
2: 在Activity中进行按钮点击viewStub = (ViewStub) findViewById(R.id.mystub);
3: View view = viewStub.inflate();把ViewStub中的View增添进来
下面Demo源代码:
主Activity类:
package com.jiangqq.viewstubdemo;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewStub; import android.widget.Button;public class ViewStubActivity extends Activity { private Button btn; private ViewStub viewStub; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { viewStub = (ViewStub) findViewById(R.id.mystub); View view = viewStub.inflate(); v.setEnabled(false); } }); } }
布局文件:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击确定" /><ViewStub android:id="@+id/mystub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout="@layout/demo_viewstub" > </ViewStub></LinearLayout>demo_viewstub.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ViewStubDemo_Byjiangqq" /></LinearLayout>
效果截图:
相关文章推荐
- 【layout】Android中使用ViewStub来提高UI的加载的性能
- Android中使用ViewStub来提高UI的加载的性能
- Android优化——UI优化(三)使用ViewStub延迟加载
- Android使用ViewStub提高布局性能
- Android性能优化之加载速度优化(一)ViewStub使用
- Android中使用ViewStub提高布局性能
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 3 - 按需载入视图(ViewStub的使用方法)]
- Android-优化UI性能(4)-使用ViewStub
- 使ViewStub 来提高UI的加载的性能
- 使用ViewStub来提高加载性能吧!
- Android提高布局加载性能ViewStub的快速实战应用
- Android使用ViewStub提高布局性能
- Android中使用ViewStub提高布局性能
- Android在layout xml中使用ViewStub完成动态加载
- Android最佳性能实践——布局优化技巧之<include>、<merge>标签及仅在需要时才加载布局的:ViewStub
- Android Training - 提升布局文件的性能(Lesson 3 - 使用viewStub按需载入视图)
- Android ViewPager Fragment使用懒加载提升性能
- 【Android UI】ListView使用ViewHolder模式提高效率
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 4 - 使用ViewHolder提升ListView的性能]
- Android进阶之路 - ViewStub可需性加载的使用