Android(安卓)ViewStub 布局延迟加载
16lz
2021-01-26
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); }}
更多相关文章
- Introducing home screen widgets and the AppWidget
- 【Android】 桌面图标右上角显示数字(角标)
- Android(安卓)Bitmap太大导致ImageView不显示的问题
- Android(安卓)自定义加载框dialog
- Android(安卓)SwipeRefreshLayout+RecyclerView下拉刷新与上拉加
- android webview 加载本地文件
- android应用程序启动解决黑屏及全屏显示时因数据加载缓慢出现标
- android webview 显示不出来
- android webview的 一些设置(js java交互)