android - 工具栏是否被视为 View ?

标签 android toolbar android-framelayout

我正在研究 one of the Material Design tutorials当我注意到 one of their Fragments正在使用带有工具栏 RecyclerView 的 FrameLayout。考虑到 documentation for FrameLayout,这很奇怪有:

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view ...

  • 如果 FrameLayout 设计为仅包含一个 View ,为什么教程要将工具栏 RecyclerView 添加到 FrameLayout?
  • 工具栏本身不被视为 View 吗?

  • RecyclerView 是这里唯一在名称中明确带有 view 的对象,但如果 Toolbar 对象不是 View 又是什么?

FrameLayout 文档还有:

You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute. Child views are drawn in a stack, with the most recently added child on top.

  • 为什么文档自相矛盾?它首先(第一个引用)表示 FrameLayout 旨在容纳单个 View ,然后它稍后(第二个引用)表示可以添加多个 subview 。我是不是误解了什么是“ child ”?

  • Toolbar 和 RecyclerView 是否有效地相互堆叠?

我注意到 RecyclerView 的父布局(一个 NestedScrollView)使用了 android:layout_marginTop="56dp",大概是为了将它的开始移动到工具栏的末尾下方(实际上,如果这个边距设置为 0dp然后 Toolbar 会遮住一些 RecyclerView)。因此,考虑到 FrameLayout 子项的“堆叠”性质,如果我从 RecyclerView 中删除此 marginTop 属性,我希望 RecyclerView 工具栏上(因为 RecyclerView 之后添加 xml 设计中的 Toolbar)。但是,这没有用;工具栏似乎保持其在“堆栈顶部”的显着位置,可以这么说。

  • 此示例中的 FrameLayout 是否更多地用作通用容器,其中 subview 自行管理其在可视区域中的位置(考虑到 android:layout_marginTop="56dp"< 的硬编码使用 在 NestedScrollView 中)?

  • 如果工具栏一个 View ,它是否被隐含地赋予了一个权重,以便它始终保持在膨胀 View 的可视堆栈的顶部?有人可能会争辩说,这对于工具栏的目的来说是有意义的。

最佳答案

FrameLayout 可用于添加多个 subview ..

Are toolbars themselves not considered views?

所有 UI 对象,例如工具栏、按钮等在底部扩展 View 对象,其中一些可能会在层次结构中扩展 ViewGroup (ToolBar),但在底部,甚至 ViewGroup 也会扩展 View 对象。


Why does the documentation contradict itself? It first (first quote) says that the FrameLayout is designed to hold a single view, then it later (second quote) says that multiple children can be added. Am I misunderstanding what a 'child' is?

不是,不是说 FrameLayout 只能有一个 child ,而是说应该有,
所以说FrameLayout可以有多个child。
subview 也是添加到ViewGroup中的那些 View ,即顾名思义,它是一个可以容纳一组其他 View 的 View 。


If the Toolbar is a view, is it implicitly given a weight such that it always remains at the top of the viewable stack of inflated views? One could argue this makes sense for the purposes of a Toolbar.

当然,工具栏是一个 View ,或者你可以说是一个 ViewGroup,
这并不意味着它停留在 Viewable 堆栈的顶部或膨胀的 View ..
您始终可以控制要在何处显示工具栏。
之所以显示在顶部是因为通常的做法是在 View 顶部显示标题和其他选项菜单。

关于android - 工具栏是否被视为 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54059127/

相关文章:

android - 有没有办法对 setRotation 进行动画处理,或者应该将其替换为 RotateAnimation?

Android 支持 Toolbar + ActionBarDrawerToggle 不更改为箭头

工具栏中条形按钮项底部的 Swift 条

java - Android 抽屉导航上的自定义工具栏

安卓。如何动态更改整个 FrameLayout 的颜色而不仅仅是 View 背景的颜色?

java - 安卓、Java : ExecutorService execute after shutdown

java - 用于 Unity 的 Android Java USB 串行通信插件未运行

java - 仅打印数组的元素

android - 如果单击按钮,则在主要 Activity 上创建一个 slideUp 窗口

android - 当在 FrameLayout 中查看时,layout_marginLeft 在 Android API < 11 上正常工作