How can I programmatically include layout in Android?

后端 未结 3 1908
臣服心动
臣服心动 2020-12-02 06:56

I\'m looking for a way to include a layout programmatically instead of using the XML tag include like in my example:

  

        
相关标签:
3条回答
  • 2020-12-02 07:27
    ViewStub stub = (ViewStub) findViewById(R.id.text_post);
            stub.setLayoutResource(R.layout.profile_header);
            View inflated = stub.inflate();
    
    0 讨论(0)
  • 2020-12-02 07:27

    In Mono.Droid / Xamarin this worked for me:

    ViewStub stub = FindViewById<ViewStub>(Resource.Id.layout_stub);
    stub.LayoutResource = Resource.Layout.whatever_layout_you_want;
    stub.Inflate();
    
    0 讨论(0)
  • 2020-12-02 07:38

    Use a ViewStub instead of include:

    <ViewStub
        android:id="@+id/layout_stub"
        android:inflatedId="@+id/message_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.75" />
    

    Then in code, get a reference to the stub, set its layout resource, and inflate it:

    ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
    stub.setLayoutResource(R.layout.whatever_layout_you_want);
    View inflated = stub.inflate();
    
    0 讨论(0)
提交回复
热议问题