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
andactivity_main.xml
.content_main.xml
is a part of thecontent_main.xml
activity_main.xml
layout and is included using the <include> tag
在布局檔案夾中,将有兩個檔案
和activity_main.xml
。content_main.xml
是content_main.xml
activity_main.xml
布局的一部分,并包含在<include>标簽中
Following is how each of these XML layouts look like:
以下是每個XML布局的外觀:
activity_main.xml
使用Kotlin的Android CoordinatorLayout 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 it
scroll :通常為需要在螢幕外滾動的所有視圖設定此标志。 不使用此标志的視圖保持靜态,進而允許其他可滾動視圖在其後滑動
-
enterAlways: This flag ensures that any downward scroll will cause the view to become visible, enabling the quick return pattern
enterAlways :此标志確定任何向下滾動都将導緻視圖變為可見,進而啟用快速傳回模式
-
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 exiting
exitUntilCollapsed :此标志導緻視圖在退出之前一直滾動到其合攏(達到其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 view
snap :滾動結束後,如果視圖僅部分可見,則将對其進行捕捉并滾動到最接近的邊緣。 是以,這避免了視圖的中期動畫狀态
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
is used then the layout_gravity attribute would set the gravity relative to the anchor view.app_layoutAnchorGravity
is used to set the anchor view on the current child. The view would be positioned relatively.app:layout_anchor
android:layout_gravity
:指定子視圖相對于父視圖的重力。
如果使用
,則layout_gravity屬性将設定相對于錨視圖的重力。app_layoutAnchorGravity
用于設定目前子項的錨點視圖。 該視圖将相對放置。app:layout_anchor
is used to set insets on the view. That locks the certain space for the view.app:layout_insetEdge
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_dodgeInsetEdges
用于設定視圖上的插圖。 這樣就為視圖鎖定了一定的空間。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.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android: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.FloatingActionButton
android: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" />
<Button
android: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顯示在其上方。
要解決此問題,請将按鈕更改為:
<Button
android: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.FloatingActionButton
android: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.FloatingActionButton
android: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.FloatingActionButton
android: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.FloatingActionButton
android: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:
本教程到此結束。 您可以從下面的連結下載下傳項目:
AndroidlyCoordinatorLayout AndroidlyCoordinatorLayout
翻譯自: https://www.journaldev.com/37765/android-coordinatorlayout-using-kotlin