What to implement: ConstraintLayout
or CoordinatorLayout
for proper material design in android ?
CoordinatorLayout is intended to be the top-level layout for activity to manage the Behaviors e.g. interactions and animations.
ConstraintLayout's main goal is to provide a convenient way to create a flat layout with multiple children (much more powerful RelativeLayout).
So the CoordinatorLayout is to manage the complex behavior (especially animations) of your activity's components, and ConstraintLayout for components proper placement (especially list items).
CoordinatorLayout is a super-powered FrameLayout.
CoordinatorLayout is intended for two primary use cases:
- As a top-level application decor or chrome layout
- As a container for a specific interaction with one or more child views
By default, if you add multiple children to a FrameLayout, they would overlap each other. A FrameLayout should be used most often to hold a single child view. The main appeal of the CoordinatorLayout is its ability to coordinate the animations and transitions of the views within it. By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout using the CoordinatorLayout.DefaultBehavior annotation.
Behaviors may be used to implement a variety of interactions and additional layout modifications ranging from sliding drawers and panels to swipe-dismissable elements and buttons that stick to other elements as they move and animate.
ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.
There are currently various types of constraints that you can use:
- Relative positioning
- Margins
- Centering positioning
- Circular positioning
- Visibility behavior
- Dimension constraints
- Chains
- Virtual Helpers objects
- Optimizer
What to implement ConstraintLayout or CoordinatorLayout for proper material design in android ?
You may need to use both ConstraintLayout and CoordinatorLayout to build efficient UI and material animations.
A common example which uses both CoordinatorLayout and ConstraintLayout is given below for your reference.
Use Coordinatorlayout as the top-level application decor. It will usually used to layout AppBarLayout , FloatingActionButton, and the main body of your screen, say NestedScrollView. Inside the NestedScrollView use ConstraintLayout to describe the rest of the layout as a flat hierarchy.
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- Your scrolling content --> <androidx.constraintlayout.widget.ConstraintLayout ...> <!-- body of constraint layout --> <Button android:id="@+id/button" ... app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView> <com.google.android.material.appbar.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent"> <androidx.appcompat.widget.Toolbar ... app:layout_scrollFlags="scroll|enterAlways"/> <com.google.android.material.tabs.TabLayout ... app:layout_scrollFlags="scroll|enterAlways"/> </com.google.android.material.appbar.AppBarLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
Bonus point
You can use the powerful MotionLayout which is a subclass of ConstraintLayout for building animations. You may check this for a detailed example for custom animation using MotionLayout.
It seems like you (almost) always use a CoordinatorLayout
, and sometimes use a ConstraintLayout
inside. See the following resources
The codelab at https://codelabs.developers.google.com/codelabs/material-design-style/index.html#3 only uses a
CoordinatorLayout
The example android-sunflower app ("illustrating Android development best practices") uses neither for the top-level activity, but uses both inside its
fragment_plant_detail.xml
, with theConstraintLayout
being inside theCoordinatorLayout
:<layout ...> <data .../> <android.support.design.widget.CoordinatorLayout ...> <android.support.design.widget.AppBarLayout ...> <android.support.design.widget.CollapsingToolbarLayout ...> <ImageView... /> <android.support.v7.widget.Toolbar... /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView ...> <android.support.constraint.ConstraintLayout ...> <TextView.../> <TextView... /> </android.support.constraint.ConstraintLayout> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton ... /> </android.support.design.widget.CoordinatorLayout> </layout>
来源:https://stackoverflow.com/questions/40929686/constraintlayout-vs-coordinator-layout