介紹
ViewStub
是一個輕量級的View,沒有尺寸置媳,不繪制任何東西于樟,因此繪制或者移除時更省時。(ViewStub不可見拇囊,大小為0)
優(yōu)點
實現(xiàn)View的延遲加載迂曲,避免資源的浪費,減少渲染時間寥袭,在需要的時候才加載View
缺點
- ViewStub所要替代的layout文件中不能有
<merge>
標簽 - ViewStub在加載完后會被移除路捧,或者說是被加載進來的layout替換掉了
用法
<ViewStub
android:id="@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
用ViewStub加載layout文件時,可以調(diào)用 setVisibility(View.VISIBLE)
或者 inflate()
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
注意
- 一旦ViewStub visible/inflated传黄,則ViewStub將從視圖框架中移除鬓长,其id
stub_import
也會失效 - ViewStub被繪制完成的layout文件取代,并且該layout文件的root view的id是android:inflatedId指定的id
panel_import
尝江,root view的布局和ViewStub視圖的布局保持一致
實例
<!-- layout_viewstub.xml 要延遲加載的view -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/layout_viewstub_old"
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="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/darker_gray"
android:padding="5dp"
android:text="This is the layout instead of ViewStub view."/>
</LinearLayout>
<!-- act_test_viewstub.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:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/act_test_viewstub_tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/darker_gray"
android:padding="5dp"
android:text="Show ViewStub"/>
<ViewStub
android:id="@+id/act_test_viewstub_viewstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inflatedId="@+id/act_layout_viewstub_new"
android:layout="@layout/layout_viewstub"/>
<!--<include-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--layout="@layout/layout_viewstub"/>-->
</LinearLayout>
public class ViewStubTestActivity extends FragmentActivity {
private static final String TAG = "test_viewstub";
protected ViewStub mViewStub;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_test_viewstub);
mViewStub = (ViewStub) findViewById(R.id.act_test_viewstub_viewstub);
Log.e(TAG, "viewstub: " + findViewById(R.id.act_test_viewstub_viewstub));
Log.e(TAG, "layout: " + findViewById(R.id.act_layout_viewstub_new));
findViewById(R.id.act_test_viewstub_tv_show).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View layoutView;
// mViewStub.setVisibility(View.VISIBLE);
layoutView = mViewStub.inflate();
Log.e(TAG, "mViewStub: " + mViewStub);
// ViewStub在visible/inflated后會被移除涉波,所以此處為null
Log.e(TAG, "viewstub: " + findViewById(R.id.act_test_viewstub_viewstub));
// layoutView = findViewById(R.id.act_layout_viewstub_new);
Log.e(TAG, "layoutView equals finviewbyid(layout): " +
layoutView.equals(findViewById(R.id.act_layout_viewstub_new)));
Log.e(TAG, "layout: " + layoutView);
if (layoutView != null) {
// layoutView的root view id 是mViewStub inflatedId指定的ID
if (layoutView.getId() == R.id.act_layout_viewstub_new) {
Log.e(TAG, "layout root id is act_layout_viewstub_new");
} else if (layoutView.getId() == R.id.layout_viewstub_old) {
Log.e(TAG, "layout root id is layout_viewstub_old");
} else {
Log.e(TAG, "layout root id is anyone : " + layoutView.getId());
}
// layoutView的root view布局 和mViewStub的布局保持一致
int width = layoutView.getLayoutParams().width;
if (width == ViewGroup.LayoutParams.MATCH_PARENT) {
Log.e(TAG, "layout width is MATCH_PARENT");
} else if (width == ViewGroup.LayoutParams.WRAP_CONTENT) {
Log.e(TAG, "layout width is WRAP_CONTENT");
} else {
Log.e(TAG, "layout width is anyone : " + width);
}
}
}
});
}
}
運行結(jié)果.png