레이아웃에 거의 사용되지 않는 복잡한 뷰가 필요한 경우가 있습니다. 여부
실행취소, 실행취소 등 중요함이 있으므로
야기할 수 있는 경우에만 뷰를 로드하여 렌더링 속도를
확인할 수 있습니다
앱에 복잡한 뷰가 있는 경우 리소스 로드를 지연할 수 있음
향후 니즈에 맞는
ViewStub:
복잡하고 거의 사용되지 않는 뷰
ViewStub 정의
ViewStub는 다음과 같은 크기가 없는 간단한 뷰입니다.
무언가를 그리거나 레이아웃에 참여할 수 없습니다. 따라서 필요한 리소스가
확장하거나 뷰 계층 구조에 남겨두는 것입니다. 각 ViewStub에 포함되는 사항
android:layout 속성을 사용하여 확장할 레이아웃을 지정합니다.
다음 코드 스니펫은 지연된 로드를 시뮬레이션합니다. 화면이 다음과 같이 로드됩니다.
Activity 및 onCreate()에서 평소와 같은 경우
heavy_layout_we_want_to_postpone 레이아웃:
Kotlin
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)}
자바
@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);}
그림 2. 무거운 레이아웃이 보입니다.
를 통해 개인정보처리방침을 정의할 수 있습니다.
를 통해 개인정보처리방침을 정의할 수 있습니다.
<ph type="x-smartling-placeholder">
ViewStub 요소는 표시되거나 확장되면 더 이상
뷰 계층 구조입니다. 확장된 레이아웃과
이 레이아웃의 루트 뷰는 android:inflatedId에서 지정합니다.
ViewStub의 속성입니다. 지정된 ID android:id
는 ViewStubViewStub
확장될 수 있습니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],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)."]]