تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يتطلب التنسيق أحيانًا طرق عرض معقدة نادرًا ما يتم استخدامها. سواء
وهي تفاصيل العنصر أو مؤشرات التقدم أو رسائل التراجع، فيمكنك تقليل
استخدامك للذاكرة وتسريع العرض من خلال تحميل طرق العرض فقط عندما
احتاجت.
يمكنك تأجيل تحميل الموارد عندما يكون لديك طرق عرض معقدة
احتياجاتك في المستقبل من خلال تحديد
ViewStub مقابل
طرق العرض المعقدة ونادرًا ما يتم استخدامها.
تعريف ViewStub
ViewStub عبارة عن طريقة عرض خفيفة بدون أي أبعاد أخرى.
لرسم أي شيء أو المشاركة في التخطيط. ولهذا السبب، فهي تتطلّب موارد قليلة
تضخيمها وتركها في التسلسل الهرمي للعرض. يشمل كل ViewStub
السمة android:layout لتحديد التنسيق الذي سيتم تضخيمه.
افترض أن لديك تخطيطًا تريد تحميله لاحقًا في رحلة المستخدم الخاصة
التطبيق:
تنتج مقتطفات التعليمات البرمجية في القسم السابق شيئًا مثل
1:
الشكل 1. الحالة الأولية للشاشة: ViewStub هو
تخفي التصميم الثقيل
عندما تريد تحميل التنسيق الذي حدّدته السمة ViewStub،
يمكنك إما ضبطه على الوضع المرئي من خلال طلب
setVisibility(View.VISIBLE)
أو الاتصال
inflate()
يحاكي مقتطف الرمز التالي عملية تحميل مؤجلة. يتم تحميل الشاشة
المعتاد في 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)}
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);}
الشكل 2. التنسيق الكثيف مرئي.
بعد ظهور العنصر ViewStub أو تكبيره، يتوقف عن الظهور.
من التسلسل الهرمي طرق العرض. يتم استبداله بالتخطيط المضخّم، ومعرّف
تحدِّد السمة android:inflatedId طريقة العرض الجذري لهذا التنسيق.
الخاصة بـ ViewStub. رقم التعريف android:id المحدّد
تكون قيمة ViewStub صالحة فقط حتى ViewStub
التخطيط مرئي أو متضخم.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)."]]