In this tutorial, we’ll be discussing CoordinatorLayout in our Android Application using Kotlin.

在本教程中,我们将使用Kotlin在Android应用程序中讨论CoordinatorLayout。

CooridinatorLayout (CooridinatorLayout)

We have discussed FrameLayouts in the past.
CoordinatorLayout is super-powered FrameLayouts with more properties.
Properties such as:

过去我们讨论过FrameLayouts 。
CoordinatorLayout是具有更多属性的超级功能FrameLayouts。
属性例如:

  • Setting certain behaviors on the layout based on actions performed on the views.根据对视图执行的操作在布局上设置某些行为。
  • Setting views dependent on one another based on certain actions. One view acts as the layout_anchor of the other. This way we can define how one view changes when another is changed.根据某些操作设置相互依赖的视图。 一个视图充当另一个视图的layout_anchor。 这样,我们可以定义一个视图在另一个视图改变时如何改变。
    • Create a fresh new android studio project and choose Basic Activity Type. Do not forget to enable Kotlin Support!

      创建一个全新的android studio项目,然后选择基本活动类型。 不要忘记启用Kotlin支持!

      In your layout folder, you’ll have two files activity_main.xml and content_main.xml.
      content_main.xml is a part of the activity_main.xml layout and is included using the <include> tag

      在布局文件夹中,将有两个文件activity_main.xmlcontent_main.xml
      content_main.xmlactivity_main.xml布局的一部分,并包含在<include>标签中

      Following is how each of these XML layouts look like:

      以下是每个XML布局的外观:

      activity_main.xml

      activity_main.xml

      The CoordinatorLayout by default consists of an AppBarLayout and a FloatingActionButton.
      These are a part of the material design support library.

      默认情况下,CoordinatorLayout由AppBarLayout和FloatingActionButton组成。
      这些是材料设计支持库的一部分。

      AppBarLayout (AppBarLayout)

      The AppBarLayout is used to hold the Toolbar. Those the Toolbar would animate the way the AppBarLayout would want it to.
      AppBarLayout in itself is a vertical LinearLayout.
      To provide it’s child views the desired scrolling behaviour, we need to set the app:layout_scrollFlags on the child view.

      AppBarLayout用于保存工具栏。 那些工具栏将使AppBarLayout想要的方式具有动画效果。
      AppBarLayout本身是垂直的LinearLayout。
      为了向子视图提供所需的滚动行为,我们需要在子视图上设置app:layout_scrollFlags。

      app:layout_scrollFlags can be set on the Toolbar. It can have the following properties,

      app:layout_scrollFlags可以在工具栏上设置。 它可以具有以下属性,

      • scroll: This flag is generally set for all views that need to scroll off-screen. Views that don’t use this flag remain static allowing the other scrollable views to slide behind itscroll :通常为需要在屏幕外滚动的所有视图设置此标志。 不使用此标志的视图保持静态,从而允许其他可滚动视图在其后滑动
      • enterAlways: This flag ensures that any downward scroll will cause the view to become visible, enabling the quick return patternenterAlways :此标志确保任何向下滚动都将导致视图变为可见,从而启用快速返回模式
      • enterAlwaysCollapsed: An additional flag for ‘enterAlways’ which modifies the returning view to only initially scroll back to it’s collapsed height.enterAlwaysCollapsed :'enterAlways'的附加标志,用于修改返回的视图,使其仅最初滚动回到其折叠高度。
      • exitUntilCollapsed: This flag causes the view to be scrolled until it is collapsed (its minHeight is reached) before exitingexitUntilCollapsed :此标志导致视图在退出之前一直滚动到其合拢(达到其minHeight)为止
      • snap: Upon a scroll ending, if the view is only partially visible then it will be snapped and scrolled to it’s the closest edge. Hence this avoids mid-animation states of a viewsnap :滚动结束后,如果视图仅部分可见,则将对其进行捕捉并滚动到最接近的边缘。 因此,这避免了视图的中期动画状态

      content_main.xml

      content_main.xml

      Ignore the ConstraintLayout for now. We’ll discuss it in a later tutorial.
      Notice the app:layout_behavior="@string/appbar_scrolling_view_behavior".
      Its the standard scrolling behaviour that scrolls the content underneath the AppBarLayout.

      现在忽略ConstraintLayout。 我们将在以后的教程中进行讨论。
      注意app:layout_behavior="@string/appbar_scrolling_view_behavior"
      它是在AppBarLayout下面滚动内容的标准滚动行为。

      Back to the activity_main.xml let’s look at some of the important XML Attributes

      回到activity_main.xml,让我们看一些重要的XML属性

      重要的XML属性 (Important XML Attributes)

      android:layout_gravity : Specifies the gravity of the child view relative to the parent.
      If app_layoutAnchorGravity is used then the layout_gravity attribute would set the gravity relative to the anchor view.
      app:layout_anchor is used to set the anchor view on the current child. The view would be positioned relatively.

      android:layout_gravity :指定子视图相对于父视图的重力。
      如果使用app_layoutAnchorGravity ,则layout_gravity属性将设置相对于锚视图的重力。
      app:layout_anchor用于设置当前子项的锚点视图。 该视图将相对放置。

      app:layout_insetEdge is used to set insets on the view. That locks the certain space for the view.
      app:layout_dodgeInsetEdges is used to set whether the view is willing to move away from it’s one side when needed. We can pass start, end, top or bottom.

      app:layout_insetEdge用于设置视图上的插图。 这样就为视图锁定了一定的空间。
      app:layout_dodgeInsetEdges用于设置视图在需要时是否愿意从一侧移开。 我们可以传递开始,结束,顶部或底部。

      FloatingActionButton has a default layout behaviour where it shifts up to give space to the SnackBar when pressed.
      FloatingActionButton具有默认的布局行为,该行为会在按下时向上移动以为SnackBar留出空间。

      layout_insetEdge和layout_dodgeInsetEdges (layout_insetEdge and layout_dodgeInsetEdges)

      Let’s add another button in our activity_main.xml CoordinatorLayout.

      让我们在activity_main.xml 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"tools:context=".MainActivity"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay" /></android.support.design.widget.AppBarLayout><include layout="@layout/content_main" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|end"android:layout_margin="@dimen/fab_margin"app:srcCompat="@android:drawable/ic_dialog_email" /><Buttonandroid:layout_width="wrap_content"android:text="WATCH ME"android:layout_gravity="bottom|start"android:layout_height="wrap_content" /></android.support.design.widget.CoordinatorLayout>

As you see the Button is hidden and the SnackBar is displayed over it.
To overcome this issue change the button to:

如您所见,按钮是隐藏的,并且SnackBar显示在其上方。
要解决此问题,请将按钮更改为:

<Buttonandroid:layout_width="wrap_content"android:text="WATCH ME"android:layout_gravity="bottom|start"app:layout_dodgeInsetEdges="bottom"android:layout_height="wrap_content" />

So app:layout_dodgeInsetEdges is set to bottom which displaces the bottom side of the button from its position when the Snackbar is displayed.

因此,将app:layout_dodgeInsetEdges设置为bottom,这将在显示Snackbar时将按钮的底部从其位置移开。

Set layout_insetEdges to bottom on the Button and you’ll see that the FloatingActionButton is no longer visible in the same position. This is because the Button locks the bottom part of the layout.
将layout_insetEdges设置为Button的底部,您将看到FloatingActionButton在相同位置不再可见。 这是因为Button锁定了布局的底部。

layout_anchor和layout_anchor重力 (layout_anchor and layout_anchorGravity)

<?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"tools:context=".MainActivity"><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="@dimen/fab_margin" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab1"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_anchor="@id/fab"app:layout_anchorGravity="left|top"android:layout_margin="@dimen/fab_margin" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab2"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_anchor="@id/fab1"app:layout_anchorGravity="end|top"android:layout_margin="@dimen/fab_margin" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab3"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_anchor="@id/fab2"android:layout_gravity="top"app:layout_anchorGravity="end|top"android:layout_margin="@dimen/fab_margin" /></android.support.design.widget.CoordinatorLayout>

With this, we’ve covered all the major XML Attributes present in a CoordinatorLayout.

至此,我们涵盖了CoordinatorLayout中存在的所有主要XML属性。

The MainActivity.kt is unchanged and looks like the following:

MainActivity.kt保持不变,如下所示:

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

AndroidlyCoordinatorLayoutAndroidlyCoordinatorLayout

翻译自: https://www.journaldev.com/37765/android-coordinatorlayout-using-kotlin

使用Kotlin的Android CoordinatorLayout相关推荐

  1. 第14章 使用Kotlin 进行 Android 开发

    2019独角兽企业重金招聘Python工程师标准>>> 第14章 使用Kotlin 进行 Android 开发 根据Realm Report (2017-Q4,https://rea ...

  2. android底部滑出view,Android CoordinatorLayout与NestedScrollView基于Behavior几行代码实现底部View滑入滑出...

    Android CoordinatorLayout与NestedScrollView基于Behavior几行代码实现底部View滑入滑出 在CoordinatorLayout的Behavior出现之前 ...

  3. 用Kotlin写Android Gradle脚本

    Android应用开发中,离不开Gradle脚本的构建.大部分Android开发同学忽视了脚本的力量,甚至有很大一部分同学不知道Gradle脚本是什么,用什么语言编写的:当然,也有相当一部分同学知道G ...

  4. Kotlin for Android

    在Google IO 2017 大会上,Google将 Kotlin列为 Android官方开发语言,Android Studio 3.0 也默认集成了Kotlin插件. Android Studio ...

  5. 如果你现在学Android---学习使用Kotlin进行Android开发

    原文地址: http://www.eoeandroid.com/thread-902176-1-1.html?_dsign=650ea146 之前写了一篇<如果你现在学Android–写给新手的 ...

  6. android 触摸 卡顿,Android CoordinatorLayout(五) 严重的卡顿BUG

    这章来讲一个重大的问题,解决卡顿,我不敢保证我的方法是最优而且对所以都管用,但是至少会比之前的滑动顺畅. 如果你用我Android CoordinatorLayout(三)中写的demo,你会发现一个 ...

  7. 用Kotlin开发android平台语音识别语义理解应用

    用Kotlin开发android平台语音识别,语义理解应用(olamisdk) 转载请注明CSDN博文地址:http://blog.csdn.net/ls0609/article/details/75 ...

  8. Kotlin on Android 开发环境介绍

    Kotlin 被 Google 采纳为 Android 开发一级编程语言,到现在也一年多了,我们团队从去年 10 月份开始部分项目尝试用 Kotlin 开发,到现在决定推广到全部项目,因为一旦用上 K ...

  9. 用Kotlin开发android平台语音识别,语义理解应用(olamisdk)

    本文使用Kotlin开发Android平台的一个语音识别方面的应用,用的是欧拉密开放平台olamisdk. 1.Kotlin简介 Kotlin是由JetBrains创建的基于JVM的编程语言,Inte ...

最新文章

  1. python公共操作(运算符(+、*、in、not in)、公共方法(len()、del、max()、min()、range()、enumerate())、类型转换(tuple、list、set))
  2. 四位专家谈:数字医学中的因果关系
  3. 如何成为一名软件架构师?
  4. 柴油发电机组的基本结构及工作特性
  5. idea svn查看提交人_svn 常规操作
  6. 熟悉html css,编写HTML和CSS的前端开发中不一定熟悉JavaScript
  7. linux7给用户授权,CentOS7 添加新用户并授权
  8. Hashtable几种常用的遍历方法
  9. 属性值动态调整_【第1603期】CSS 自定义属性:使用篇
  10. 管理感悟:再好的设计,不如能运行的原型
  11. ng4 html路由跳转,Angular4.x通过路由守卫实现动态跳转界面步骤详解
  12. 我来告诉你,一个草根程序员如何逆袭,成功进入BAT!
  13. Quorum企业以太坊搭建区块链记录系列
  14. Weighted Interval Scheduling
  15. Windows XP系统常见故障简单排除
  16. Vue启动项目报错:Can‘t resolve ‘xxx‘ in ‘D:\briup\vue_test\node_modules\send‘
  17. Zephyr events
  18. 搭建Web环境初识JSP
  19. Windows彻底卸载VMWare虚拟机
  20. 《数字中国建设整体布局规划》充分发挥“数据”生产要素:形成横向打通、纵向贯通、协调有力的一体化推进格局...

热门文章

  1. [转载] Python 列表表达式
  2. [转载] python字典查询功能_Python中的字典功能
  3. vivado中FIFO IP核的Standard FIFO和First-word-Fall-Through模式的仿真比较
  4. verilog读入.txt的有符号十进制数,把有符号十进制数写入到.txt文件中
  5. Linux文件上传下载sz 和 rz 命令
  6. JavaScript正则表达式补充
  7. android 软件apk自动更新实现注意点!!
  8. 【配置】Spring Struts配置信息
  9. Oracle 单实例 迁移到 RAC 实例 -- 使用RMAN 异机恢复
  10. C# webbrowser 忽略页面错误