Android佈局總結二:ViewStub總結

Android佈局總結二:ViewStub總結

ViewStub是一個輕量級的的View,繼承於ViewGroup,沒有任何尺寸,不繪制任何東西,因此繪制或者移除時更省時。(ViewStub不可見,大小為0)

優點

實現View的延遲加載,避免資源的浪費,減少渲染時間,在需要的時候才加載View

缺點

ViewStub所要替代的layout文件中不能有標簽 ViewStub在加載完後會被移除,或者說是被加載進來的layout替換掉瞭

用法

    

用ViewStub加載layout文件時,可以調用 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視圖的佈局保持一致

實例

 
 
 
         
 
 
 
     
     
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); 
                    } 
                } 
            } 
        }); 
    } 
}

 

這裡寫圖片描述

 

You May Also Like