يتيح لك شريط التطبيقات إضافة أزرار لإجراءات المستخدم. تتيح لك هذه الميزة وضع
أهم الإجراءات للسياق الحالي في أعلى التطبيق.
على سبيل المثال، قد يعرض تطبيق تصفّح الصور زرَّي مشاركة وإنشاء
ألبوم في أعلى الشاشة عندما ينظر المستخدم إلى ألبوم الصور. عندما ينظر المستخدِم إلى صورة فردية، قد يعرض التطبيق زرَّي القطع والفلتر.
المساحة في شريط التطبيقات محدودة. إذا كان التطبيق يعلن عن إجراءات أكثر من تلك التي يمكن أن تتسع
في شريط التطبيق، يرسل شريط التطبيق الإجراءات الزائدة إلى قائمة overflow.
يمكن للتطبيق أيضًا تحديد أنّ إجراءً معيّنًا يظهر دائمًا في القائمة الكاملة بدلاً من عرضه في شريط التطبيق.
الشكل 1. رمز إجراء في تطبيق "الميزات الجديدة في Android"
إضافة أزرار إجراءات
يتم تحديد جميع أزرار الإجراءات والعناصر الأخرى المتاحة في قائمة الإجراءات الإضافية في مرجع قائمة بتنسيق XML. لإضافة
إجراءات إلى شريط الإجراءات، أنشئ ملف XML جديدًا في دليل
res/menu/ الخاص بمشروعك.
أضِف عنصر
<item>
لكل عنصر تريد تضمينه في شريط الإجراءات، كما هو موضّح في المثال التالي لملف XML الخاص بالقائمة:
تحدِّد السمة app:showAsAction ما إذا كان الإجراء
يظهر كزر في شريط التطبيق. في حال ضبط
app:showAsAction="ifRoom"، كما هو الحال في مثال الرمز البرمجي لسمة
favorite، يتم عرض الإجراء كزر إذا كان هناك مساحة في
شريط التطبيق. إذا لم تكن هناك مساحة كافية، يتم إرسال الإجراءات الزائدة إلى
قائمة الخيارات الإضافية. في حال ضبط app:showAsAction="never"، كما هو الحال في إعدادات رمز المثال، يتم إدراج الإجراء دائمًا في
القائمة الكاملة ولا يتم عرضه في شريط التطبيق.
يستخدم النظام رمز الإجراء كزرّ للإجراء إذا كان الإجراء معروضًا
في شريط التطبيق. يمكنك العثور على العديد من الرموز المفيدة في مجموعة رموز Material.
الردّ على الإجراءات
عندما يختار المستخدم أحد عناصر شريط التطبيق، يستدعي النظام onOptionsItemSelected()
طريقة الاستدعاء
في نشاطك ويمرّر MenuItem
كائنًا لتحديد العنصر الذي تم النقر عليه. في عملية تنفيذ
onOptionsItemSelected()، استخدِم الأسلوب
MenuItem.getItemId()
لتحديد العنصر الذي تم النقر عليه. يتطابق المعرّف الذي يتم عرضه مع القيمة التي تذكرها في سمة
android:id عنصر <item> المقابل.
على سبيل المثال، يتحقّق مقتطف الرمز البرمجي التالي من الإجراء الذي يختاره المستخدم.
إذا لم تتعرّف الطريقة على إجراء المستخدم، ستستدعي طريقة الفئة الأساسية:
Kotlin
overridefunonOptionsItemSelected(item:MenuItem)=when(item.itemId){R.id.action_settings->{// User chooses the "Settings" item. Show the app settings UI.true}R.id.action_favorite->{// User chooses the "Favorite" action. Mark the current item as a// favorite.true}else->{// The user's action isn't recognized.// Invoke the superclass to handle it.super.onOptionsItemSelected(item)}}
Java
@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){switch(item.getItemId()){caseR.id.action_settings:// User chooses the "Settings" item. Show the app settings UI.returntrue;caseR.id.action_favorite:// User chooses the "Favorite" action. Mark the current item as a// favorite.returntrue;default:// The user's action isn't recognized.// Invoke the superclass to handle it.returnsuper.onOptionsItemSelected(item);}}
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ 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,["# Add and handle actions\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Dynamic top app bar →](/develop/ui/compose/components/app-bars-dynamic) \n\nThe app bar lets you add buttons for user actions. This feature lets you put\nthe most important *actions* for the current context at the top of the app.\nFor example, a photo browsing app might show *share* and *create\nalbum* buttons at the top when the user is looking at their photo roll. When\nthe user looks at an individual photo, the app might show *crop* and\n*filter* buttons.\n\nSpace in the app bar is limited. If an app declares more actions than can fit\nin the app bar, the app bar sends the excess actions to an *overflow* menu.\nThe app can also specify that an action always shows in the overflow menu,\ninstead of displaying on the app bar.\n**Figure 1.** An action icon in the \"Now in Android\" app.\n\nAdd action buttons\n------------------\n\nAll action buttons and other items available in the action overflow are\ndefined in an XML\n[menu resource](/guide/topics/resources/menu-resource). To add\nactions to the action bar, create a new XML file in your project's\n`res/menu/` directory.\n\nAdd an\n[`\u003citem\u003e`](/guide/topics/resources/menu-resource#item-element)\nelement for each item you want to include in the action bar, as shown in the\nfollowing sample menu XML file: \n\n```xml\n\u003cmenu xmlns:android=\"http://schemas.android.com/apk/res/android\" \nxmlns:app=\"http://schemas.android.com/apk/res-auto\"\u003e\n\n \u003c!-- \"Mark Favorite\", must appear as action button if possible. --\u003e\n \u003citem\n android:id=\"@+id/action_favorite\"\n android:icon=\"@drawable/ic_favorite_black_48dp\"\n android:title=\"@string/action_favorite\"\n app:showAsAction=\"ifRoom\"/\u003e\n\n \u003c!-- Settings, must always be in the overflow. --\u003e\n \u003citem android:id=\"@+id/action_settings\"\n android:title=\"@string/action_settings\"\n app:showAsAction=\"never\"/\u003e\n\n\u003c/menu\u003e\n```\n\nThe `app:showAsAction` attribute specifies whether the action is\nshown as a button on the app bar. If you set\n`app:showAsAction=\"ifRoom\"`---as in the example code's\n*favorite* action---the action displays as a button if there is room in\nthe app bar for it. If there isn't enough room, excess actions are sent to the\noverflow menu. If you set `app:showAsAction=\"never\"`---as in the\nexample code's *settings* action---the action is always listed in the\noverflow menu and not displayed in the app bar.\n\nThe system uses the action's icon as the action button if the action displays\nin the app bar. You can find many useful icons in\n[Material Icons](https://www.google.com/design/icons/).\n\nRespond to actions\n------------------\n\nWhen the user selects one of the app bar items, the system calls your\nactivity's\n[onOptionsItemSelected()](/reference/android/app/Activity#onOptionsItemSelected(android.view.MenuItem))\ncallback method and passes a\n[MenuItem](/reference/android/view/MenuItem) object\nto indicate which item was tapped. In your implementation of\n`onOptionsItemSelected()`, call the\n[MenuItem.getItemId()](/reference/android/view/MenuItem#getItemId())\nmethod to determine which item was tapped. The ID returned matches the value you\ndeclare in the corresponding `\u003citem\u003e` element's\n`android:id` attribute.\n\nFor example, the following code snippet checks which action the user selects.\nIf the method doesn't recognize the user's action, it invokes the superclass\nmethod: \n\n### Kotlin\n\n```kotlin\noverride fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {\n R.id.action_settings -\u003e {\n // User chooses the \"Settings\" item. Show the app settings UI.\n true\n }\n\n R.id.action_favorite -\u003e {\n // User chooses the \"Favorite\" action. Mark the current item as a\n // favorite.\n true\n }\n\n else -\u003e {\n // The user's action isn't recognized.\n // Invoke the superclass to handle it.\n super.onOptionsItemSelected(item)\n }\n}\n```\n\n### Java\n\n```java\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n switch (item.getItemId()) {\n case R.id.action_settings:\n // User chooses the \"Settings\" item. Show the app settings UI.\n return true;\n\n case R.id.action_favorite:\n // User chooses the \"Favorite\" action. Mark the current item as a\n // favorite.\n return true;\n\n default:\n // The user's action isn't recognized.\n // Invoke the superclass to handle it.\n return super.onOptionsItemSelected(item);\n\n }\n}\n```"]]