Android support library coordinatorLayout and other layout frameworks

Android's support library provides a series of functional components for layout and UI design.One of the important components is CoordinatorLayout, which can help us build a complex application interface and provide some powerful interaction and animation effects.This article will compare the characteristics and advantages of CoordinatorLayout and other commonly used layout frameworks, and provide some Java code examples for the case. 1. LinearLayout Linearlayout is one of the most basic layout frameworks of Android. It arranges the sub -view in order in order or vertical direction.Compared with CoordinatorLayout, Linearlayout's layout ability is relatively weak, and it cannot handle complex view layer overlap and interactive effects. For example, when a "card -type" layout needs to be implemented, the card view can be dragged and placed in different positions, CoordinatorLayout is more suitable for processing such interactions.Below is an example code that uses CoordinatorLayout to achieve this effect: <android.support.design.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"> <android.support.v7.widget.CardView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-The content in the card-> </android.support.v7.widget.CardView> </android.support.design.widget.CoordinatorLayout> 2. RelativeLayout RelativeLayout allows us to locate relative to other views in the view level.Although RelativeLayout has a stronger layout function, when dealing with complex interaction and animation, it still cannot be comparable to CoordinatorLayout. A common case of use is to make a stretched head view. When the user pulls the screen, the head view will gradually become higher.The following is an example code that uses CoordinatorLayout to achieve this effect: <android.support.design.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"> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-Rolling content-> </android.support.v4.widget.NestedScrollView> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" /> </android.support.design.widget.CoordinatorLayout> 3. ConstraintLayout ConstraintLayout is a relatively new layout framework, which is particularly useful when dealing with complex layout relationships.Compared with CoordinatorLayout, ConstraintLayout provides more flexible and fine control methods, and can automatically adapt to different screen size and direction. However, when involving complex interaction and animation, CoordinatorLayout still has advantages.The following is a sample code that can implement a panel that can be opened and folded using CoordinatorLayout: <android.support.design.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"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways"/> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-Rolling content-> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:src="@drawable/ic_add" app:layout_anchor="@id/toolbar" app:layout_anchorGravity="bottom|right|end" /> </android.support.design.widget.CoordinatorLayout> Summarize: Although LinearLayout, RelativeLayout, and ConstraintLayout are more common and flexible than CoordinatorLayout in terms of layout functions, CoordinatorLayout has unique advantages in processing complex interaction and animation.It can help us achieve more complex and attractive application interface.Therefore, in the scene involving complex interaction and animation, we should give priority to using CoordinatorLayout to build our layout.