什么是CoordinatorLayout?

问题描述 投票:89回答:6

仅查看了新的Android支持设计库的演示应用程序。由Chris Banes在github上提供。在整个应用程序中,CoordinatorLayout被大量使用。此外,在FloatingActionButton中使用时,许多支持设计库类(例如SnackBarAppBarLayoutCoordinatorLayout等)的行为也有所不同。

[有人可以说明CoordinatorLayout是什么,以及它与Android中其他ViewGroup的不同之处,或者至少提供了学习CoordinatorLayout的正确方法。

android android-support-library android-coordinatorlayout android-design-library
6个回答
37
投票

这是您在寻找的文档中的内容:

设计库引入了CoordinatorLayout,这是一种布局,可以对子视图之间的触摸事件进行额外的控制,这是设计库中许多组件所利用的。

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

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

希望这会有所帮助:)


32
投票

什么是CoordinatorLayout?不要让这个花哨的名字欺骗了您,它不过是类固醇上的FrameLayout

为了最好地了解CoordinatorLayout是/做什么,您必须首先了解/牢记对坐标的含义。

[如果您用谷歌这个词

坐标

这就是您得到的:

enter image description here

我认为这些定义有助于描述CoordinatorLayout本身的功能及其内部视图的行为。

<<

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

拖动,轻扫,猛击或其他任何手势。

[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的布局

打开:

enter image description here

已关闭:

enter image description here

12
投票
要注意的另一点。由于OP特别询问

7
投票
CoordinatorLayout本质上是具有很多功能的框架布局,从名称上就可以明显看出,它使子元素之间的协调自动进行,并有助于构建漂亮的视图。可以在Google Play商店应用中看到其实现。工具栏如何折叠和更改颜色。

5
投票
快速概述Android Documentation中有用的内容:

0
投票
要注意的一件事是CoordinatorLayout对FloatingActionButton或AppBarLayout工作没有任何天生的了解-它仅以Coordinator.Behavior的形式提供了一个附加API,该子API允许子视图更好地控制触摸事件和手势,以及声明彼此之间的依赖关系,并通过onDependentViewChanged()接收回调。
© www.soinside.com 2019 - 2024. All rights reserved.