什么是CoordinatorLayout?

刚看了一下新的Android支持devise库的演示应用程序。 它由github上的Chris Banes提供。 通过应用程序, CoordinatorLayout被重用。 此外,许多支持devise库类(如FloatingActionButtonSnackBarAppBarLayout等)在CoordinatorLayout使用时的行为AppBarLayout有所不同。

有人可以请教什么是CoordinatorLayout什么是不同于Android中的其他ViewGroup的,或者至less提供学习CoordinatorLayout正确path。

这是你正在寻找。

从文档

devise库引入了CoordinatorLayout ,这是一个布局,它提供了子视图之间的触摸事件的一个附加级别的控制,这是Design库中许多组件所利用的。

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

在此链接中,您将看到上述所有视图的演示video。

希望这可以帮助 :)

另外需要注意的一点。 自OP特意问

此外,在CoordinatorLayout中使用时,许多支持devise的libabry类(如FloatingActionButton,SnackBar,AppBarLayout等)的行为也会有所不同。

我想这是因为这个。

CoordinatorLayout是一个超级供电的FrameLayout。

FABbutton,SnackBar在FrameLayout的概念上工作,并且由于CoordinatorLayout本身具有FrameLayout的function,所以它可能使其他视图的行为不同!

什么是协调员布局? 不要让这个奇特的名字欺骗你,它不过是一个类固醇的FrameLayout

为了最好地理解CoordinatorLayout是什么,你必须首先理解/记住坐标是什么意思。

如果你Google的话

坐标

这是你得到的:

在这里输入图像说明

我认为这些定义有助于描述CoordinatorLayout自己做什么,以及其中的视图如何performance。

CoordinatorLayout(ViewGroup)将布局的不同元素(子视图)带入一个和谐或有效的关系:

在CoordinatorLayout的帮助下,孩子的观点协调一致地执行真棒行为,如

拖动,滑动,甩动,或任何其他手势。

CoordinatorLayout中的视图通过指定这些行为来与其他人协商以有效地协作

CoordinatorLayout是Material Design的超酷function,有助于创build有吸引力和协调的布局。

所有你需要做的是将你的子视图包裹在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

打开:

在这里输入图像说明

closures:

在这里输入图像说明

CoordinatorLayout本质上是框架布局,具有很多function,这从名称是显而易见的,它自动协调孩子之间的协调,并帮助build立美丽的景色。 它的实现可以在Google Play商店App中看到。工具栏如何折叠和更改颜色。

简要介绍一下Android文档中有用的内容:

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

例如,如果你想要你的工具栏折叠或隐藏。 Google通过引入AppBarLayout&CollapsingToolbarLayout,使得它非常简单,在CoordinatorLayout下,这两种工具都是最好的。

另一个最常用的情况是,当你想要一个FloatingActionButton坚持到你的CollapsingToolbar的底部和移动它,把它们放在一个coordinatorLayout下,并使用app:layout_anchor="@id/YourAppBarId"胶水(!)和app:layout_anchorGravity="bottom|end"作为位置就足以让你看到神奇的作品!

通过使用这种布局作为上下文,子视图将会有更好的协作,并以一种智能的方式运行,因为它们将通过CoordinatorLayout上下文相互了解,这意味着您的FloatingActionbutton不会再被快餐栏等重叠。

这些只是对最有用部分的快速总结,所以如果您想在节省更多时间来制作应用程序,那么在这个主题中进行更深入的研究是值得的。

请参阅Google滚动视图活动模板