overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_old_xml)Handler(Looper.getMainLooper()).postDelayed({findViewById<View>(R.id.stub_import).visibility=View.VISIBLE// Or val importPanel: View = findViewById<ViewStub>(R.id.stub_import).inflate()},2000)}
Java
@OverridevoidonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState);setContentView(R.layout.activity_old_xml);Handler(Looper.getMainLooper()).postDelayed({findViewById<View>(R.id.stub_import).visibility=View.VISIBLE// Or val importPanel: View = findViewById<ViewStub>(R.id.stub_import).inflate()},2000);}
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Load views on demand\n\nSometimes your layout requires complex views that are rarely used. Whether\nthey are item details, progress indicators, or undo messages, you can reduce\nmemory usage and speed up rendering by loading the views only when they're\nneeded.\n\nYou can defer loading resources when you have complex views that your app\nneeds in the future by defining a\n[ViewStub](/reference/android/view/ViewStub) for\ncomplex and rarely used views.\n\nDefine a ViewStub\n-----------------\n\n`ViewStub` is a lightweight view with no dimension that doesn't\ndraw anything or participate in the layout. As such, it requires few resources\nto inflate and leave in a view hierarchy. Each `ViewStub` includes\nthe `android:layout` attribute to specify the layout to inflate.\n\nSuppose you have a layout you want to load later in the user journey of your\napp: \n\n```transact-sql\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cFrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cImageView\n android:src=\"@drawable/logo\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"/\u003e\n\u003c/FrameLayout\u003e\n```\n\nYou can postpone loading using the following `ViewStub`. To make\nit show or load anything, you must make it show the referred layout: \n\n```xml\n\u003cFrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\nandroid:id=\"@+id/root\"\nandroid:layout_width=\"match_parent\"\nandroid:layout_height=\"match_parent\"\u003e\n\n\u003cViewStub\n android:id=\"@+id/stub_import\"\n android:inflatedId=\"@+id/panel_import\"\n android:layout=\"@layout/heavy_layout_we_want_to_postpone\"\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:layout_gravity=\"bottom\" /\u003e\n\u003c/FrameLayout\u003e\n```\n\nLoad the ViewStub layout\n------------------------\n\nThe code snippets in the previous section produce something like figure\n1:\n**Figure 1.** Initial state of the screen: the `ViewStub` is hiding the heavy layout.\n\nWhen you want to load the layout specified by the `ViewStub`,\neither set it to visible by calling\n[setVisibility(View.VISIBLE)](/reference/android/view/View#setVisibility(int))\nor call\n[inflate()](/reference/android/view/ViewStub#inflate()).\n\nThe following code snippet simulates a postponed load. The screen loads as\nusual in the `Activity` and `onCreate()`, then it shows\nthe `heavy_layout_we_want_to_postpone` layout: \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_old_xml)\n\n Handler(Looper.getMainLooper())\n .postDelayed({\n findViewById\u003cView\u003e(R.id.stub_import).visibility = View.VISIBLE\n \n // Or val importPanel: View = findViewById\u003cViewStub\u003e(R.id.stub_import).inflate()\n }, 2000)\n}\n```\n\n### Java\n\n```java\n@Override\nvoid onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_old_xml);\n\n Handler(Looper.getMainLooper())\n .postDelayed({\n findViewById\u003cView\u003e(R.id.stub_import).visibility = View.VISIBLE\n \n // Or val importPanel: View = findViewById\u003cViewStub\u003e(R.id.stub_import).inflate()\n }, 2000);\n}\n```\n**Figure 2.** The heavy layout is visible. **Note:** The `inflate()` method returns the inflated `View` after it's complete, so you don't need to call [findViewById()](/reference/android/app/Activity#findViewById(int)) if you need to interact with the layout.\n\nOnce visible or inflated, the `ViewStub` element is no longer part\nof the view hierarchy. It is replaced by the inflated layout, and the ID for the\nroot view of that layout is specified by the `android:inflatedId`\nattribute of the `ViewStub`. The ID `android:id` specified\nfor the `ViewStub` is valid only until the `ViewStub`\nlayout is visible or inflated.\n| **Note:** A drawback of `ViewStub` is that it doesn't support the `\u003cmerge\u003e` tag in the layouts to be inflated.\n\nFor more information about this topic, see the blog post\n[Optimize\nwith stubs](http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-with.html)."]]