[Android]Android布局优化之 ViewStub

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述[Android]Android布局优化之 ViewStub相关的知识,希望能为你提供帮助。
 
转载请标注:转载于http://www.cnblogs.com/Liuyt-61/p/6602926.html 
----------------------------------------------------------------
> 使用ViewStub惰性加载
作用:ViewStub标签同include标签一样可以用来引入一个外部布局,不同的是,ViewStub引入的布局默认不会扩张,既不会占用显示,也不会占用位置,从而在解析layout时节省cpu和内存。

common_text.xml< ?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:orientation="vertical" > < TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="隐藏内容" /> < /LinearLayout> ------------------------------------------------ main.xml< ?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:orientation="vertical" > < include layout="@layout/common_title" /> < FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" > < TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="正文内容" android:textSize="16sp" /> < include layout="@layout/common_progress" /> < /FrameLayout> < Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示隐藏内容" /> < ViewStub android:id="@+id/viewStub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout="@layout/common_text" /> < /LinearLayout> -------------------------------------- MainActivity.javapackage com.Liuyt.s03_e22_include; 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 MainActivity extends Activity {private Button btn; private ViewStub viewStub; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button); viewStub = (ViewStub) findViewById(R.id.viewStub); btn.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) { // TODO Auto-generated method stub viewStub.inflate(); } }); } }

【[Android]Android布局优化之 ViewStub】 

    推荐阅读