Android Lollipop 工具栏与自定义视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26508934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:38:19  来源:igfitidea点击:

Android Lollipop Toolbar vs Custom view

androidandroid-5.0-lollipopandroid-toolbar

提问by TomCB

I just read about the new Toolbar in Android Lollipop. It inherits from ViewGroup.

我刚刚阅读了 Android Lollipop 中的新工具栏。它继承自 ViewGroup。

Why are the advantages of using the new Toolbar over, say, a LinearLayout to place your own views?

为什么使用新工具栏比使用 LinearLayout 放置您自己的视图有优势?

回答by Jared Burrows

Explanation:

解释:

You should start using a ToolBarinstead of the ActionBar. This is directly taken from the Android Developer documentation. It allows more flexibility than the normal ActionBar; however, retains some the more prominent features such as adding a Logo, supports "up" Navigation, inflating menusand actionviewssuch as the SearchView.

您应该开始使用 aToolBar而不是ActionBar。这直接取自 Android 开发人员文档。它比普通的更灵活ActionBar;但是,保留了一些更突出的功能,例如添加徽标,支持“向上”导航充气菜单和操作视图(例如 SearchView)。

Tutorials:

教程:

Implement yourself(by Chris Banes)

实现自己(由 Chris Banes)

Collapsing Toolbar, FloatingActionButton, NavigationView, Snackbar(by Chris Banes)

折叠工具栏, FloatingActionButton, NavigationView, Snackbar(by Chris Banes)

Documentation:

文档:

Normal Toolbar(API 21+)

普通Toolbar(API 21+)

AppCompat Support v7 Toolbar(API 7+)

AppCompat 支持 v7 Toolbar(API 7+)

A standard toolbar for use within application content.

A Toolbaris a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.

Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:

A navigation button.This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar.

A branded logo image.This may extend to the height of the bar and can be arbitrarily wide.

A title and subtitle.The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.

One or more custom views.The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.

An action menu.The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

在应用程序内容中使用的标准工具栏。

一个工具栏是应用程序布局中使用的动作条的概括。虽然操作栏传统上是由框架控制的活动不透明窗口装饰的一部分,但工具栏可以放置在视图层次结构中的任意嵌套级别。应用程序可以选择使用 setActionBar() 方法将工具栏指定为活动的操作栏。

Toolbar 支持比 ActionBar 更集中的功能集。从头到尾,工具栏可能包含以下可选元素的组合:

一个导航按钮。这可能是向上箭头、导航菜单切换、关闭、折叠、完成或应用程序选择的其他字形。此按钮应始终用于访问工具栏容器内的其他导航目的地及其指定内容,或者以其他方式离开工具栏指定的当前上下文。

品牌徽标图像。这可以延伸到条的高度并且可以是任意宽的。

一个标题和副标题。标题应该是工具栏在导航层次结构中的当前位置以及其中包含的内容的路标。副标题(如果存在)应指示有关当前内容的任何扩展信息。如果应用程序使用徽标图像,则应强烈考虑省略标题和副标题。

一个或多个自定义视图。应用程序可以向工具栏添加任意子视图。它们将出现在布局中的这个位置。如果子视图的 Toolbar.LayoutParams 指示重力值为 CENTER_HORIZONTAL,则在测量所有其他元素后,视图将尝试在工具栏中剩余的可用空间内居中。

一个动作菜单。操作菜单将固定在工具栏的末尾,提供一些频繁、重要或典型的操作以及用于其他操作的可选溢出菜单。在现代 Android UI 中,开发人员应该更多地依赖于工具栏的视觉上不同的配色方案,而不是他们的应用程序图标。在 API 21 和更新版本的设备上不鼓励使用应用程序图标和标题作为标准布局。