How to use View Stub in android

前端 未结 3 602
野趣味
野趣味 2020-12-13 02:07

I want to use ViewStub in android, so please help me. I have created

ViewStub stub = new ViewStub;
View inflated = stub.inflate(); 

How to

3条回答
  •  臣服心动
    2020-12-13 02:44

    Like the documentation says, ViewStub is a View that is inflated lazily.

    You can declare a ViewStub in an XML file like this:

     
    

    The android:layout attribute is a reference to the View that will be inflated next to a call of inflate(). So

    ViewStub stub = (ViewStub) findViewById(R.id.stub);
    View inflated = stub.inflate();
    

    When the method inflate() is invoked the ViewStub is removed from its parent and replaced with the right View (the root view of mySubTree layout).

    If you want to do this progammatically then your code should be something like:

    ViewStub stub = new ViewStub(this);
    stub.setLayoutResource(R.layout.mySubTree);
    stub.inflate(); 
    

提交回复
热议问题