Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 什么是协调布局?_Android_Android Support Library_Android Coordinatorlayout_Android Design Library - Fatal编程技术网

Android 什么是协调布局?

Android 什么是协调布局?,android,android-support-library,android-coordinatorlayout,android-design-library,Android,Android Support Library,Android Coordinatorlayout,Android Design Library,刚刚看了一下新安卓支持设计库的演示应用程序。它是由克里斯·贝恩斯在上星期六提供的。通过该应用程序,CoordinatorLayout被大量使用。此外,许多支持设计库类,如FloatingActionButton、SnackBar、AppBarLayout等,在协调布局中使用时表现不同 有人能告诉我们什么是协调布局,它与android中的其他视图组有什么不同,或者至少提供学习协调布局的正确途径吗 来自文档 设计库引入了CoordinatorLayout,该布局为子视图之间的触摸事件提供了额外的控制

刚刚看了一下新安卓支持设计库的演示应用程序。它是由克里斯·贝恩斯在上星期六提供的。通过该应用程序,
CoordinatorLayout
被大量使用。此外,许多支持设计库类,如
FloatingActionButton
SnackBar
AppBarLayout
等,在
协调布局中使用时表现不同


有人能告诉我们什么是协调布局,它与android中的其他视图组有什么不同,或者至少提供学习协调布局的正确途径吗

来自文档

设计库引入了
CoordinatorLayout
,该布局为子视图之间的触摸事件提供了额外的控制级别,设计库中的许多组件都利用了该布局

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

希望这有帮助:)

基本上是框架布局,具有许多功能,从名称上看很明显,它自动化了子对象之间的协调,并有助于构建美丽的视图。它的实现可以在Google Play Store应用程序中看到。工具栏是如何折叠和改变颜色的


最好的事情是我们给予它的直接或间接后代的行为。你一定在滚动时看到了所有的UI进入运动状态。这种行为很可能正在发挥其魔力。

另外需要注意的一点。因为OP特别要求

此外,许多支持设计的libabry类 FloatingActionButton、SnackBar、AppBarLayout等的行为不同 当在坐标布局内部使用时

<?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>
我猜是因为这个

CoordinatorLayout是一种功能强大的框架布局。


FAB按钮,SnackBar使用FrameLayout的概念,并且由于CoordinatorLayout本身具有FrameLayout的功能,它可能会使其他视图的行为有所不同

要快速截图,了解哪些内容在中很有用:

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

例如,如果希望工具栏折叠或隐藏。谷歌通过引入AppBarLayout和CollasingToolbarLayout使其变得非常简单,这两种布局在协调布局下都能发挥最佳效果

<?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>
另一种最常用的情况是,当您希望浮动操作按钮粘在折叠工具栏的底部并随它移动时,将它们放在协调布局下,并使用
app:layout\u-anchor=“@id/YourAppBarId”
进行粘合(!)和
app:layout\u-anchorGravity=“bottom | end”
as的位置足以让您看到神奇的效果

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

这些只是对最有用部分的快速总结,因此,如果你想在应用程序动画制作上节省更多时间,就值得深入研究一下这个主题


请参见

什么是协调布局?别让这个花哨的名字骗了你,它只不过是一个类固醇的框架布局而已

为了更好地理解
协调人或布局的含义,您必须首先理解/牢记协调的含义

如果你用谷歌搜索这个词

协调

这就是你得到的:

我认为这些定义有助于描述CoordinatorLayout自身的功能以及其中的视图的行为

协调或布局(一个视图组)带来了(一个视图组)的一个(协调或布局(一个视图组)带来了不同元素(儿童视图)的不同元素(儿童视图)带来了不同元素(儿童视图)的不同元素(儿童视图)的不同元素(一个视图组)带来了(一个视图组)带来了不同元素(儿童视图)的不同元素(儿童视图)的不同元素(儿童视图)带来了(儿童视图)的不同元素(儿童视图)的以下)的不同元素(儿童视图)的不同元素(儿童视图)带来了一个(儿童视图)的以下以下)的不同元素(儿童视图)的不同的不同元素(儿童视图)的不同元素(儿童视图)的不同元素(儿童视图)带来了一个(儿童视图)的以下)的不同的以下以下以下)的不同的不同的以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下的不同的不同元素(儿童视图)的不同元素(儿童视图)的不同的不同的不同元素(儿童视图)的不同的n̶i̶z̶A̶t̶i̶o̶n̶̶̶̶̶n̶̶̶̶̶̶̶

在协调布局的帮助下,儿童视图和谐地协同工作,以实施令人敬畏的行为,例如

拖动、滑动、投掷或任何其他手势

协调布局中的视图通过指定这些视图与其他视图进行协商,以便有效地协同工作

协调布局是材料设计的一个超级酷的特性,有助于创建有吸引力和协调的布局

您所要做的就是将子视图包装到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>

关闭:


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

视图可以通过使用CoordinatorLayout.DefaultBehavior(YourView.Behavior.class)注释声明默认行为,或者通过使用app:layout\u Behavior=“com.example.app.YourView$Behavior”属性在布局文件中设置默认行为。该框架使任何视图都可以与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>
现在可用! 设计库现在可用,因此请确保在SDK管理器中更新Android支持存储库。然后,可以开始使用带有单个新依赖项的设计库:

编译'com.android.support:design:22.2.0' 请注意,由于设计库依赖于Support v4和AppCompat支持库,因此在添加设计库依赖项时将自动包括这些支持库。我们还注意到这些新的小部件可以在AndroidStudio布局编辑器的设计视图中使用(在CustomView下找到它们),这为您预览这些新组件提供了一种更简单的方法

设计库,AppCompat,