في أبسط صوره، يعرض شريط الإجراءات عنوان النشاط من جانب
والقائمة الكاملة على الآخر. حتى في هذا الشكل الأساسي، يوفر شريط التطبيق
معلومات مفيدة للمستخدمين ويمنح تطبيقات Android مظهرًا وأسلوبًا متسقًا.
الشكل 1. شريط تطبيق يتضمّن رمز إجراء في تطبيق "الميزات الجديدة في Android"
تحتوي جميع الأنشطة التي تستخدم المظهر التلقائي على ActionBar كشريط
تطبيقات. تتم إضافة ميزات شريط التطبيقات إلى ActionBar الأصلي في إصدارات Android المختلفة. ونتيجةً لذلك، يتصرف ActionBar الأصلي بشكل مختلف
حسب إصدار Android الذي يستخدمه الجهاز.
من ناحية أخرى، تتم إضافة ميزات إلى إصدار Toolbar من مكتبة AndroidX AppCompat،
ما يعني أنّ هذه الميزات متاحة على الأجهزة التي تستخدم مكتبات AndroidX.
لهذا السبب، استخدِم فئة Toolbar من مكتبة AndroidX لتنفيذ
أشرطة التطبيقات في الأنشطة. يؤدي استخدام شريط الأدوات في مكتبة AndroidX إلى جعل سلوك
تطبيقك متسقًا على أوسع نطاق من الأجهزة.
إضافة شريط أدوات إلى نشاط
توضّح هذه الخطوات كيفية إعداد Toolbar كشريط تطبيقات نشاطك:
في بيان التطبيق، اضبط عنصر
<application>
على استخدام أحد مظاهر
NoActionBar
لنظام AppCompat، كما هو موضّح في المثال التالي. يؤدي استخدام أحد هذه المظاهر إلى منع
تطبيقك من استخدام فئة ActionBar الأصلية لتوفير شريط التطبيق.
ضَع شريط الأدوات في أعلى
تنسيق النشاط، لأنّك تستخدمه
كشريط تطبيق.
في طريقة
onCreate()
لنشاطك، استخدِم طريقة
setSupportActionBar()
لنشاطك وأرسِل شريط أدوات النشاط، كما هو موضّح في المثال التالي. تضبط هذه الطريقة شريط الأدوات كشريط التطبيق للنشاط.
Kotlin
overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_my)// The Toolbar defined in the layout has the id "my_toolbar".setSupportActionBar(findViewById(R.id.my_toolbar))}
يحتوي تطبيقك الآن على شريط إجراءات أساسي. يحتوي شريط الإجراءات تلقائيًا على اسم
التطبيق وقائمة الخيارات الإضافية التي تحتوي في البداية على عنصر الإعدادات.
يمكنك إضافة المزيد من الإجراءات إلى شريط الإجراءات والقائمة الكاملة، كما هو موضّح في
إضافة الإجراءات والتعامل معها.
استخدام طرق أداة شريط التطبيقات
بعد ضبط شريط الأدوات كشريط تطبيق للنشاط، يمكنك الوصول إلى مثيل ActionBar
الذي يوفّر أدوات برمجية في مكتبة AndroidX. يتيح لك هذا النهج تنفيذ إجراءات مفيدة، مثل إخفاء شريط التطبيق وعرضه.
لاستخدام طرق الأداة ActionBar، يمكنك استدعاء getSupportActionBar()
method للنشاط. تُعرِض هذه الطريقة مرجعًا إلى عنصر ActionBar في AppCompat.
بعد الحصول على هذا المرجع، يمكنك استدعاء أي من طرق ActionBar
لضبط شريط التطبيق. على سبيل المثال، لإخفاء شريط التطبيقات، يمكنك طلب
ActionBar.hide().
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ 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,["# Set up the app bar\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[App Bar →](/develop/ui/compose/components/app-bars) \n\nIn its most basic form, the action bar displays the title for the activity on one\nside and an overflow menu on the other. Even in this basic form, the app bar provides\nuseful information to users and gives Android apps a consistent look and feel.\n**Figure 1.** An app bar with an action icon in the \"Now in Android\" app.\n\nAll activities that use the default theme have an\n[ActionBar](/reference/android/app/ActionBar) as an app\nbar. App bar features are added to the native `ActionBar` over various\nAndroid releases. As a result, the native `ActionBar` behaves differently\ndepending on what version of Android a device is using.\n\nOn the other hand, features are added to the AndroidX AppCompat library's version of\n[Toolbar](/reference/androidx/appcompat/widget/Toolbar),\nwhich means those features are available on devices that use the AndroidX libraries.\n\nUse the AndroidX library's `Toolbar` class to implement your activities'\napp bars for this reason. Using the AndroidX library's toolbar makes your app's\nbehavior consistent across the widest range of devices.\n\nAdd a Toolbar to an Activity\n----------------------------\n\nThese steps describe how to set up a `Toolbar` as your activity's app bar:\n\n1. Add the AndroidX library to your project, as described in [AndroidX overview](/jetpack/androidx).\n2. Make sure the activity extends [AppCompatActivity](/reference/androidx/appcompat/app/AppCompatActivity): \n\n ### Kotlin\n\n ```kotlin\n class MyActivity : AppCompatActivity() {\n // ...\n }\n ```\n\n ### Java\n\n ```java\n public class MyActivity extends AppCompatActivity {\n // ...\n }\n ```\n | **Note:** Make this change for every activity in your app that uses a `Toolbar` as an app bar.\n3. In the app manifest, set the [`\u003capplication\u003e`](/guide/topics/manifest/application-element) element to use one of AppCompat's [NoActionBar](/reference/android/R.style#Theme_DeviceDefault_Light_NoActionBar) themes, as shown in the following example. Using one of these themes prevents the app from using the native `ActionBar` class to provide the app bar. \n\n ```xml\n \u003capplication\n android:theme=\"@style/Theme.AppCompat.Light.NoActionBar\"\n /\u003e\n ```\n4. Add a `Toolbar` to the activity's layout. For example, the following layout code adds a `Toolbar` and gives it the appearance of floating above the activity: \n\n ```xml\n \u003candroidx.appcompat.widget.Toolbar\n android:id=\"@+id/my_toolbar\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"?attr/actionBarSize\"\n android:background=\"?attr/colorPrimary\"\n android:elevation=\"4dp\"\n android:theme=\"@style/ThemeOverlay.AppCompat.ActionBar\"\n app:popupTheme=\"@style/ThemeOverlay.AppCompat.Light\"/\u003e\n ```\n\n See the\n [Material Design specification](https://material.io/design/components/app-bars-bottom.html)\n for recommendations regarding app bar elevation.\n\n Position the toolbar at the top of the activity's\n [layout](/guide/topics/ui/declaring-layout), since you are using\n it as an app bar.\n5. In the activity's [onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle)) method, call the activity's [setSupportActionBar()](/reference/androidx/appcompat/app/AppCompatActivity#setSupportActionBar(androidx.appcompat.widget.Toolbar)) method and pass the activity's toolbar, as shown in the following example. This method sets the toolbar as the app bar for the activity. \n\n ### Kotlin\n\n ```kotlin\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_my)\n // The Toolbar defined in the layout has the id \"my_toolbar\".\n setSupportActionBar(findViewById(R.id.my_toolbar))\n }\n ```\n\n ### Java\n\n ```java\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_my);\n Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);\n setSupportActionBar(myToolbar);\n }\n ```\n\nYour app now has a basic action bar. By default, the action bar contains the name\nof the app and an overflow menu, which initially contains the **Settings** item.\nYou can add more actions to the action bar and the overflow menu, as described in\n[Add and handle actions](/develop/ui/views/components/appbar/actions).\n\nUse app bar utility methods\n---------------------------\n\nOnce you set the toolbar as an activity's app bar, you have access to the utility\nmethods provided by the AndroidX library's\n[ActionBar](/reference/androidx/appcompat/app/ActionBar)\nclass. This approach lets you do useful things, like hide and show the app bar.\n\nTo use the `ActionBar` utility methods, call the activity's\n[getSupportActionBar()](/reference/androidx/appcompat/app/AppCompatActivity#getSupportActionBar())\nmethod. This method returns a reference to an AppCompat `ActionBar` object.\nOnce you have that reference, you can call any of the `ActionBar` methods\nto adjust the app bar. For example, to hide the app bar, call\n[ActionBar.hide()](/reference/androidx/appcompat/app/ActionBar#hide())."]]