ViewStub本身只占一点内存空间

在调用inflate后 才加载layout属性所指向的layout, 以替换当前ViewStub所占的位置

在某些 需要选择显示何种布局时,就可以使用ViewStub来延迟加载

注意:inflate只能有一次


layout: activity_view_stub.xml

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/btn_show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="show"        android:text="显示viewstub指向的layout"/>    <Button        android:id="@+id/btn_hide"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/btn_show"        android:onClick="hide"        android:text="隐藏viewstub指向的layout"/>    <ViewStub        android:id="@+id/vs_stub"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_below="@id/btn_show"        android:layout="@layout/rorate_bitmap"/></RelativeLayout>


public class ViewStubActivity extends Activity {      ViewStub mViewStub;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_view_stub);        mViewStub = (ViewStub) findViewById(R.id.vs_stub);    }    View inflate;    public void show(View view) {        if (inflate == null)            inflate = mViewStub.inflate();        else            inflate.setVisibility(View.VISIBLE);    }    public void hide(View view) {        if (inflate == null)            inflate = mViewStub.inflate();        inflate.setVisibility(View.GONE);    }}



更多相关文章

  1. Introducing home screen widgets and the AppWidget
  2. 【Android】 桌面图标右上角显示数字(角标)
  3. Android(安卓)Bitmap太大导致ImageView不显示的问题
  4. Android(安卓)自定义加载框dialog
  5. Android(安卓)SwipeRefreshLayout+RecyclerView下拉刷新与上拉加
  6. android webview 加载本地文件
  7. android应用程序启动解决黑屏及全屏显示时因数据加载缓慢出现标
  8. android webview 显示不出来
  9. android webview的 一些设置(js java交互)

随机推荐

  1. Android(安卓)Shape自定义纯色圆角按钮
  2. Android(安卓)L中的RecyclerView 、CardV
  3. Android学习备忘006——Manifest.xml
  4. android典型代码系列(十一)------判断某
  5. Android TextView使用HTML处理字体样式、
  6. Android !No Launcher activity found!错误
  7. android 基础学习(6)-----sqlite3查看表
  8. android 4.0以上无法收到开机广播
  9. IntelliJ IDEA破解版 2017.3.4 Ultimate
  10. Android应用实例之----天气预报程序