什么是CoordinatorLayout?

jim*_*251 89 android android-support-library android-design-library android-coordinatorlayout

刚看了一下新的Android支持设计库的演示应用程序.它由Chris Banes在github上提供.通过应用程序,CoordinatorLayout使用沉重.此外,许多支持设计库类如FloatingActionButton,SnackBar,AppBarLayout等行为有所内使用时CoordinatorLayout.

有人可以CoordinatorLayout说明一下它是什么以及它与ViewGroupandroid中的其他s 有何不同,或者至少提供正确的学习途径CoordinatorLayout.

Qad*_*ain 36

在这里,你正在寻找.

来自docs

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

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

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

希望这可以帮助 :)

  • 链接坏了。 (4认同)

ojo*_*ifu 31

什么是CoordinatorLayout?不要让花哨的名字欺骗你,它只不过是类固醇的FrameLayout

为了最好地理解什么CoordinatorLayout是/做什么,你必须首先理解/记住它对协调的意义.

如果你谷歌这个词

坐标

这就是你得到的:

在此输入图像描述

我认为这些定义有助于描述CoordinatorLayout自身的作用以及其中的视图如何表现.

甲CoordinatorLayout(一个的ViewGroup)带来的不同元素(子查看)中(络合物活动或组织)̶布局成一个和谐或有效的关系式:

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

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

CoordinatorLayout内部的视图与其他人协商,以便通过指定这些行为有效地协同工作

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>
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

这给了我们一个布局,可以滚动以折叠工具栏并隐藏FloatingActionButton

打开:

在此输入图像描述

关闭:

在此输入图像描述

  • 相对或约束布局在视图之间也没有关系吗?如果一个视图向上移动,则相关视图向上移动。为什么协调员更好? (2认同)
  • @Snake 1. CoordinatorLayout 有助于在另一个视图上排列一个视图,这是 ConstraintLayout 或relativelayout 都无法做到的。2. 它还有助于动画视图的转换。一个很好的例子是 Whatsapp 应用程序中“查看联系人”活动/片段的转换。 (2认同)

cap*_*wag 12

还有一点需要注意.由于OP特别要求

此外,许多支持设计libabry类(如FloatingActionButton,SnackBar,AppBarLayout等)在CoordinatorLayout中使用时表现不同.

我猜是因为这个原因.

CoordinatorLayout是一个超级动力的FrameLayout.

FAB Button,SnackBar适用于FrameLayout的概念,由于CoordinatorLayout本身具有FrameLayout的功能,它可能会使其他视图的行为不同!


Uza*_*air 7

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

关于CoordinatorLayout的最好的事情是我们给它的直接或间接后代的行为.你必须看到滚动所有的UI进入运动.这种行为极有可能发挥其魔力.


Ami*_*imi 5

快速快速拍摄Android文档中有用的内容:

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

例如,如果您希望ToolBar折叠或隐藏.谷歌通过引入AppBarLayout和CollapsingToolbarLayout让它变得非常简单,它们在CoordinatorLayout下都能发挥最佳效果.

另一个最常用的情况是当你想要一个FloatingActionButton粘在你的CollapsingToolbar的底部并随身携带它,将它们放在一个coordinatorLayout下并app:layout_anchor="@id/YourAppBarId"用于胶水(!),app:layout_anchorGravity="bottom|end"因为位置足以让你看到神奇的工作!

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

这些只是大多数有用部分的快速摘要,因此如果您想在动画应用程序中节省更多时间,那么在该主题中进行更深入的研究将是值得的.

查看Google滚动视图活动模板