ActionBar 与工具栏或 ActionBar 和工具栏

ActionBar vs Toolbar or ActionBar and Toolbar

我已了解 AppCompat 库中的工具栏及其所有功能。我在 android 开发者博客 (here) 中注意到的一些事情是:

Toolbar is fully supported in AppCompat and has feature and API parity with the framework widget.

他们还提到我们可以对其外观进行更多控制。

但是,当我在 Android Studio 中添加 activity 时,我得到了这个:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_add_contacts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout> 

这里Toolbar在AppBar里面。 (ActionBar 和 AppBar 是一样的,不是吗?)那有什么用。在一些博客中,我还了解到 AppBar 可以替换为设计支持库的工具栏。

通常您会在工具栏后看到 content_layout。所以我们将在工具栏之后有下面一行。

<include layout="@layout/content_myactivity" />

所以,一旦我遇到问题,内容位于工具栏上方(仅使用工具栏,不在应用栏内)隐藏了整个工具栏,并且仍然可以点击。所以我不得不在代码中将工具栏移动到我的内容下方,以使其显示在我的内容上方。

那么,用什么?工具栏?应用栏布局? AppBarLayout 中的工具栏?

每一个的预期用途是什么?

更新:

所以我已经在 activity_layout 文件中添加了工具栏。那为什么要用setSupportActionBar(toolbar);设置工具栏和添加主题AppTheme.NoActionBar。这是 Android Studio 中所有活动的正常行为。

禁用window操作栏有什么用
<item name="windowActionBar">false</item>

并设置为 setSupportActionBar(toolbar) ?

AppBarLayout 是一个容器,您可以在其中放置 ToolBarTabLayout 或其他东西。所有这些都将显示在屏幕顶部,无论您对其余内容使用何种布局。如果需要,您可以在不使用 AppBarLayout 的情况下使用 Toolbar,但是您需要将其包含在您使用其余内容的 ViewGroup 中。并将它放在它的底部,这样它就不会被其他东西覆盖。

AppBar 使您免于此,并提供了一些附加功能,例如滚动行为。写成here btw.

另请注意,AppBarLayout 在很大程度上取决于在 CoordinatorLayout 中用作直接子代。如果您在另一个 ViewGroup 中使用 AppBarLayout,它的大部分功能将不起作用。

AppBarLayout 是一个垂直 LinearLayout,它实现了 material 设计应用栏概念的许多功能,即滚动手势。

Children 应该通过 setScrollFlags(int) 和关联的布局 xml 属性提供他们想要的滚动行为:app:layout_scrollFlags.

此视图在很大程度上取决于在 CoordinatorLayout 中用作直接 child。如果您在另一个 ViewGroup 中使用 AppBarLayout,它的大部分功能将无法使用。

AppBarLayout 还需要一个单独的滚动兄弟,以便知道何时滚动。绑定是通过 AppBarLayout.ScrollingViewBehavior 行为 class 完成的,这意味着您应该将滚动视图的行为设置为 AppBarLayout.ScrollingViewBehavior 的实例。包含完整 class 名称的字符串资源可用。

您还可以像下面这样实现 child of AppBarLayout

<android.support.design.widget.CoordinatorLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

 <android.support.v4.widget.NestedScrollView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:layout_behavior="@string/appbar_scrolling_view_behavior">

     <!-- Your scrolling content -->

 </android.support.v4.widget.NestedScrollView>

 <android.support.design.widget.AppBarLayout
         android:layout_height="wrap_content"
         android:layout_width="match_parent">

     <android.support.v7.widget.Toolbar
             ...
             app:layout_scrollFlags="scroll|enterAlways"/>

     <android.support.design.widget.TabLayout
             ...
             app:layout_scrollFlags="scroll|enterAlways"/>

 </android.support.design.widget.AppBarLayout>

来源:AppBarLayout

另见 Structure of AppBarLayout