What to implement: ConstraintLayout
or CoordinatorLayout
for proper material design in android ?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
@Darish has a great, comprehensive answer to this one. I second everything he said and wanted to just add a little info. In my experience, a Constraint layout as the parent view is good enough most of the time. When you need to bring in the Coordinator Layout is when you have specific behaviors that you want to manage (for example Bottom Sheets). Coordinator Layout is more trouble than it's worth if you won't be using the behavior capabilities of it or if you are trying to mess with multiple views as CoordinatorLayout acts as a "super-powered FrameLayout".
I wrote a blog post a while ago with illustrations about the differences and usages of Coordinator vs. Constraint layout. Check it out here if you are interested.
I would also second the plug for MotionLayout as a great comprehensive way to add animations to your layouts without too much additional code! This Google Developers series with examples is a great way to get started with MotionLayout.
CoordinatorLayout is a super-powered FrameLayout.
CoordinatorLayout
is intended for two primary use cases:By default, if you add multiple children to a
FrameLayout
, they would overlap each other. AFrameLayout
should be used most often to hold a single child view. The main appeal of theCoordinatorLayout
is its ability to coordinate the animations and transitions of the views within it. By specifying Behaviors for child views of aCoordinatorLayout
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 aCoordinatorLayout
using theCoordinatorLayout.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 is a super-powered ViewGroup similar to a RelativeLayout, but more flexible than RelativeLayout.
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 thanRelativeLayout
and easier to use with Android Studio's Layout Editor.ConstraintLayout
can be used anywhere, you don't need any other ViewGroup likeRelativeLayout
,LinearLayout
orFrameLayout
once you start usingConstraintLayout
.There are currently various types of constraints that you can use:
What to implement
ConstraintLayout
orCoordinatorLayout
for proper material design in android ?You may need to use both
ConstraintLayout
andCoordinatorLayout
to build efficient UI and material animations.A common example which uses both
CoordinatorLayout
andConstraintLayout
is given below for your reference.Use
Coordinatorlayout
as the top-level application decor. It will usually used to layoutAppBarLayout
,FloatingActionButton
, and the main body of your screen, sayNestedScrollView
. Inside theNestedScrollView
useConstraintLayout
to describe the rest of the layout as a flat hierarchy.What does the above snippet do? here you go.
androidx.coordinatorlayout.widget.CoordinatorLayout
as the root layout. And we putandroidx.core.widget.NestedScrollView
andcom.google.android.material.appbar.AppBarLayout
as direct children.We defined
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
attribute forandroidx.core.widget.NestedScrollView
. This is the key point. We defined a behavior for theNestedScrollView
. That is we are telling the Coordinator layout that theNestedScrollView
depends on theAppBarLayout
.We placed the
ConstraintLayout
inside theNestedScrollView
to make it scrollable. As we already discussed, the ConstraintLayout is used to align child views with in the bounds of the ConstraintLayout.Can I add ConstraintLayout inside another ConstraintLayout?
Of course yes, You can use any combination to align views as per your design requirements.
Can I add CoordinatorLayout inside another CoordinatorLayout ?
That is not a usual practice. the most common use case of CoordinatorLayout is as a the top-level application decor to coordinate between other direct children. But yes, if you really want to nest the CoordinatorLayout, you can do so by creating a custom CoordinatorLayout which extends the
CoordinatorLayout
and implementsNestedScrollingChild
to pass the scroll events to the parent 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 usingMotionLayout
.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).
It seems like you (almost) always use a
CoordinatorLayout
, and sometimes use aConstraintLayout
inside. See the following resourcesThe 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
: