什么是 CoordinatorLayout?

What is CoordinatorLayout?

刚刚查看了新 Android 支持设计库的演示应用程序。它由 Chris Banes 在 github 上提供。在整个应用程序中,CoordinatorLayout 被大量使用。此外,许多支持设计库 类,例如 FloatingActionButtonSnackBarAppBarLayout 等,在 CoordinatorLayout 中使用时表现不同。

有人可以阐明什么是 CoordinatorLayout 以及它与 android 中的其他 ViewGroup 有何不同,或者至少提供学习 [=10] 的正确途径=].

这就是你要找的。

来自文档

Design 库引入了 CoordinatorLayout,这是一种布局,它提供了对子视图之间触摸事件的额外级别控制,Design 库中的许多组件都利用了这一点。

https://android-developers.googleblog.com/2015/05/android-design-support-library.html

在此link您将看到上述所有视图的演示视频。

希望这对您有所帮助:)

CoordinatorLayout 本质上是具有许多功能的框架布局,从名称中就可以看出,它自动协调其子项之间并帮助构建美丽的视图。它的实现可以在 Google Play Store App.How 中看到,工具栏折叠并改变颜色。

CoordinatorLayout 最好的一点是我们赋予其直接或间接后代的行为。您一定已经看到滚动时所有 UI 都开始运动了。这种行为很可能正在发挥它的魔力。

还有一点要注意。由于 OP 特别询问

Also, many of the support design libabry classes like FloatingActionButton, SnackBar, AppBarLayout etc. behaves differently when used inside CoordinatorLayout.

我猜是因为这个。

CoordinatorLayout is a super-powered FrameLayout.

FAB Button、SnackBar基于FrameLayout的概念,由于CoordinatorLayout本身具有FrameLayout的功能,可能会使其他view表现不同!

快速了解 Android Documentation 中有用的内容:

使用 CoordinatorLayout 来简单地控制视图的关系行为,

例如,如果您希望工具栏折叠或隐藏。 Google 通过引入 AppBarLayout 和 CollapsingToolbarLayout 使其变得非常简单,它们在 CoordinatorLayout 下工作得最好。

另一种最常用的情况是当您希望 FloatingActionButton 粘在 CollapsingToolbar 的底部并随它移动时,将它们放在 coordinatorLayout 下并使用 app:layout_anchor="@id/YourAppBarId" 作为胶水 (!) 和app:layout_anchorGravity="bottom|end"因为位置足够让你看到神奇的作品!

通过将此布局用作上下文,子视图将具有更好的协作并以智能方式运行,因为它们将通过 CoordinatorLayout 上下文相互了解,这意味着您的 FloatingAction 按钮将不再与小吃店等

这些只是对最有用部分的快速总结,因此如果您想节省更多时间来为您的应用制作动画,那么深入了解该主题是值得的。

查看 Google Scrolling view activity template

什么是 CoordinatorLayout?别被花哨的名字给骗了,它只不过是类固醇的 FrameLayout

要更好地理解 CoordinatorLayout is/does,您必须首先 understand/bear 记住坐标的含义。

如果你 Google 这个词

Coordinate

这是你得到的:

我认为这些定义有助于描述 CoordinatorLayout 自身的作用以及其中的视图的行为方式。

A CoordinatorLayout (a ViewGroup) brings the different elements (child Views) of a (̶a̶ ̶c̶o̶m̶p̶l̶e̶x̶ ̶a̶c̶t̶i̶v̶i̶t̶y̶ ̶o̶r̶ ̶a̶n̶ ̶o̶r̶g̶a̶n̶i̶z̶a̶t̶i̶o̶n̶)̶ layout into a harmonious or efficient relationship:

在 CoordinatorLayout 的帮助下,子视图可以和谐地协同工作以实现令人敬畏的行为,例如

drags, swipes, flings, or any other gestures.

CoordinatorLayout 中的视图通过指定这些 Behaviors

与其他视图协商以便有效地协同工作

CoordinatorLayout 是 Material Design 的一项超酷功能,有助于创建有吸引力且协调的布局。

您所要做的就是将您的子视图包装在 CoordinatorLayout 中。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout        
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:context="com.byte64.coordinatorlayoutexample.ScollingActivity">

 <android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">



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

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@android:drawable/ic_dialog_email" />

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

和content_scrolling:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView     
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:layout_behavior="@string/appbar_scrolling_view_behavior"
 tools:context="com.byte64.coordinatorlayoutexample.ScollingActivity"
 tools:showIn="@layout/activity_scolling">

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:text="@string/large_text" />

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

这为我们提供了一个可以滚动以折叠工具栏并隐藏 FloatingActionButton 的布局

打开:

关闭:

需要注意的一件事是,CoordinatorLayout 对 FloatingActionButton 或 AppBarLayout 的工作没有任何天生的理解 - 它只是以 Coordinator.Behavior 的形式提供了一个额外的 API,这允许子视图更好地控制触摸事件和手势,以及声明彼此之间的依赖关系并通过 onDependentViewChanged() 接收回调。

视图可以通过使用 CoordinatorLayout.DefaultBehavior(YourView.Behavior.class) 注释声明默认行为,或者通过 app:layout_behavior="com.example.app.YourView$Behavior" 属性在布局文件中设置它.该框架使任何视图都可以与 CoordinatorLayout 集成。

现在可用! 设计库现已可用,因此请务必更新 SDK 管理器中的 Android 支持存储库。然后您可以开始使用具有单个新依赖项的设计库:

编译'com.android.support:design:22.2.0' 请注意,由于设计库依赖于 Support v4 和 AppCompat 支持库,因此当您添加设计库依赖项时,它们将自动包含在内。我们还注意确保这些新小部件可以在 Android Studio 布局编辑器的设计视图中使用(在 CustomView 下找到它们),让您可以更轻松地预览其中一些新组件。

设计库、AppCompat 和所有 Android 支持库都是重要的工具,可提供构建现代、美观的 Android 应用程序所需的构建基块,而无需从头开始构建所有内容。

CoordinatorLayout是超能力者FrameLayout

默认情况下,如果您将多个子项添加到 FrameLayout,它们会相互重叠。 FrameLayout 应该最常用于保存单个子视图。 CoordinatorLayout 的主要吸引力在于它能够协调其中视图的 动画 转换 。仅使用 XML,您可以描述一个布局,其中 FAB 移出传入 Snackbar 的方式,例如,或者有一个明显附加到另一个小部件并继续移动的 FAB(或任何其他视图) -带有小部件的屏幕。

这是主要来源tutorial