📌  相关文章
📜  Android中ActionBar和Toolbar的区别

📅  最后修改于: 2021-09-11 03:44:52             🧑  作者: Mango

操作栏

ActionBar是出现在活动屏幕顶部的元素。这是 android 应用程序的一个显着特征,它在其所有活动中都具有一致的存在。它为应用程序提供了视觉结构,并包含一些用户经常使用的元素。 Android ActionBar 由Google 于 2013 年随着 Android 3.0(API 11)的发布而推出。在此之前,这个最顶级的视觉元素的名称是AppBar 。所有使用 Android 提供的默认主题(Theme.AppCompat.Light.DarkActionBar) 的应用程序,默认都包含一个 ActionBar。但是,开发人员可以根据自己的需要以多种方式对其进行自定义。可以包含在 ActionBar 中的组件有:

  • 导航控制按钮/抽屉
  • 应用程序图标
  • 标题和副标题
  • 操作按钮
  • 动作溢出菜单

应用程序中的 ActionBar:

工具栏

工具栏是一种可以放置在活动的XML 布局中的ViewGroup。它是由Google Android 团队在Android Lollipop(API 21)发布期间引入的。 Toolbar 基本上是 ActionBar的高级继承者。它在外观和功能方面更加灵活和可定制。与 ActionBar 不同的是,它的位置不是硬编码的,即不在活动的顶部。开发人员可以根据需要将其放置在 Activity 中的任何位置,就像 android 中的任何其他 View 一样。 Toolbar 使用Android 的 Material Design主题功能,因此它提供了高达API 7(Android 2.1) 的向后兼容性。 Toolbar 支持的功能比 ActionBar 更加集中和可定制。以下是可以添加到工具栏的组件:

  • 导航按钮/抽屉或向上按钮
  • 品牌标志/应用程序图标
  • 标题和副标题
  • 操作菜单项
  • 多个自定义视图,如 TextView、ImageView 等。

应用程序中的 ActionBar(在顶部)和 Toolbar:

差异表

ActionBar

Toolbar

It is a dedicated navigation or control element present at top of each screen of the application. It is a ViewGroup that can be placed anywhere in the activity layout.
It was released along with Android 3.0 (API -11) as the successor of AppBar.   Introduced in Material design with the release of Android 5.0 i.e., lollipop(API – 21).
It is a part of the activity’s opaque window decor, thus its position is hardcoded. It is the generalized form of ActionBar that can be placed at any arbitrary level within the Component Tree hierarchy of an activity.  
There can be only one ActionBar in an activity. Developers have the choice to show or hide the ActionBar. Multiple Toolbar element having a completely different appearance and behavior can be defined within a single activity. A Toolbar can also be used as an ActionBar.
Behaves as a regular AppBar but provides scope for customization. Developers can add application logo/icon, action menu items, overflow menu, etc. Fewer lines of code are needed to make the same customization in Toolbar that is possible in ActionBar. Moreover, Toolbar provides more flexibility and control to the developers by which they can animate the elements or can apply reaction to scroll events.
Google introduced new features in ActionBar gradually. During the release of API 15, 17, and 19 several new features were added to the ActionBar. All features and functionalities were released during the launch of API 21 with the addition of material theme design
Features added at the different API level does not provide backward compatibility.  Provides backward compatibility up to API 7(Android 2.1).
想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!