در ابتدایی ترین شکل خود، نوار اکشن عنوان فعالیت را در یک طرف و منوی سرریز را در طرف دیگر نمایش می دهد. حتی در این شکل اولیه، نوار برنامه اطلاعات مفیدی را در اختیار کاربران قرار میدهد و به برنامههای اندروید ظاهر و احساسی ثابت میدهد.
شکل 1. نوار برنامه با نماد عمل در برنامه "اکنون در اندروید".
همه فعالیتهایی که از تم پیشفرض استفاده میکنند دارای ActionBar به عنوان نوار برنامه هستند. ویژگی های نوار برنامه از طریق نسخه های مختلف اندرویدی به ActionBar بومی اضافه می شود. در نتیجه ، ActionBar بومی بسته به نوع نسخه Android یک دستگاه متفاوت رفتار می کند.
از سوی دیگر، ویژگیهایی به نسخه Toolbar کتابخانه AndroidX AppCompat اضافه شده است، به این معنی که این ویژگیها در دستگاههایی که از کتابخانههای AndroidX استفاده میکنند در دسترس هستند.
به همین دلیل از کلاس Toolbar کتابخانه AndroidX برای پیاده سازی نوارهای برنامه فعالیت های خود استفاده کنید. استفاده از نوار ابزار کتابخانه AndroidX باعث می شود که رفتار برنامه شما در وسیع ترین طیف دستگاه ها سازگار باشد.
یک نوار ابزار به یک فعالیت اضافه کنید
این مراحل نحوه تنظیم Toolbar به عنوان نوار برنامه فعالیت خود شرح می دهد:
همانطور که در نمای کلی AndroidX توضیح داده شده است، کتابخانه AndroidX را به پروژه خود اضافه کنید.
در مانیفست برنامه، عنصر <application> را طوری تنظیم کنید که از یکی از تم های NoActionBar AppCompat استفاده کند، همانطور که در مثال زیر نشان داده شده است. استفاده از یکی از این تم ها مانع از استفاده برنامه از کلاس ActionBar بومی برای ارائه نوار برنامه می شود.
نوار ابزار را در بالای طرحبندی فعالیت قرار دهید، زیرا از آن به عنوان نوار برنامه استفاده میکنید.
In the activity's onCreate() method, call the activity's setSupportActionBar() method and pass the activity's toolbar, as shown in the following example. این روش نوار ابزار را به عنوان نوار برنامه برای فعالیت تنظیم می کند.
کاتلین
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))}
اکنون برنامه شما دارای یک نوار اقدام اساسی است. به طور پیش فرض، نوار اقدام حاوی نام برنامه و یک منوی سرریز است که در ابتدا حاوی آیتم تنظیمات است. میتوانید اقدامات بیشتری را به نوار عملکرد و منوی سرریز اضافه کنید، همانطور که در Add and handle actions توضیح داده شده است.
از روش های ابزار نوار برنامه استفاده کنید
هنگامی که نوار ابزار را به عنوان نوار برنامه یک فعالیت تنظیم کردید، به روش های کاربردی ارائه شده توسط کلاس ActionBar کتابخانه AndroidX دسترسی خواهید داشت. این رویکرد به شما امکان می دهد کارهای مفیدی مانند پنهان کردن و نمایش نوار برنامه را انجام دهید.
برای استفاده از متدهای کاربردی ActionBar ، متد getSupportActionBar() فعالیت را فراخوانی کنید. این روش یک مرجع را به یک شی AppCompat ActionBar برمی گرداند. هنگامی که آن مرجع را دارید، می توانید با هر یک از روش های ActionBar برای تنظیم نوار برنامه تماس بگیرید. به عنوان مثال، برای مخفی کردن نوار برنامه، ActionBar.hide() را فراخوانی کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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())."]]