ViewStub
ViewStub
布局惰性加載機制,它是不可見的占據窗口大小為0的一個View,在運行時加載布局資源,當其visible或者調用inflate()方法時,就會變為可見,并將其本身加載出來的布局傳遞給父布局,但是他只能被加載一次,重復加載會報這個錯誤 Caused by: java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent。因為他加載完本身就不存在了,自然不能調用。
通常一個布局里面對應不同狀態有好幾個布局的時候,會寫到一起,設置他們的Visibility屬性為gone或者invisible,在代碼中進行邏輯控制展現,比較麻煩,而且這樣寫雖然看不到,但扔被實例化,消耗資源,而ViewStub則進行了性能優化。
使用
首先我們在布局中定義一個ViewStub,里面有一個layout屬性,就是我們要惰性加載的布局,在這里已經定義好了,只是默認不可見,不占據空間
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ViewStubandroid:id="@+id/stub"android:layout="@layout/subtree"android:layout_width="120dip"android:layout_height="40dip" /></LinearLayout>接下來在主活動中進行處理
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ViewStub viewStub=findViewById(R.id.stub);//viewStub.setVisibility(View.VISIBLE);View inflated=viewStub.inflate();//與上一個寫法一樣效果(設置其可見與用inflate()加載一樣)//viewStub.inflate();寫第二遍會報錯,只能加載一次TextView hintText=inflated.findViewById(R.id.text);hintText.setText("qwerty");} }直接調用 viewStub.inflate()也可以,只不過用View接一下,可以繼續處理布局里面的控件。
參考: https://www.jianshu.com/p/175096cd89ac
https://blog.csdn.net/cx1229/article/details/53505903
總結
- 上一篇: Codeforces Round #29
- 下一篇: 使用TFHpple解析html