Android: what is the best practice to use CoordinatorLayout

I am new to the material design aspect of android. So please carry me. I tried to implement FAB for my home activities and then implemented a diner. Naturally, it was above my FAB. I examined the Layout coordinator and what bothers me, which one is better to use to use the CoordinatorLayout?

For example, here is the xml of my activity.

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primary_color"></android.support.v7.widget.Toolbar> <android.support.design.widget.FloatingActionButton android:id="@+id/searchfab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginBottom="20dp" android:layout_marginRight="20dp" android:src="@drawable/ic_add_black_24dp" app:fabSize="normal"> </android.support.design.widget.FloatingActionButton> </RelativeLayout> 

I watched other people add CoordinatorLayout like this.

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primary_color"></android.support.v7.widget.Toolbar> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.FloatingActionButton android:id="@+id/searchfab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="20dp" android:layout_marginRight="20dp" android:src="@drawable/ic_add_black_24dp" app:fabSize="normal"> </android.support.design.widget.FloatingActionButton> </android.support.design.widget.CoordinatorLayout> </RelativeLayout> 

My question is: can we use <android.support.design.widget.CoordinatorLayout> as the root of all elements. Completely bypassing or removing <RelativeLayout> .

So xml will look like this.

  <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primary_color"> <android.support.design.widget.FloatingActionButton android:id="@+id/searchfab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="20dp" android:layout_marginRight="20dp" android:src="@drawable/ic_add_black_24dp" app:fabSize="normal"> </android.support.design.widget.FloatingActionButton> </android.support.design.widget.CoordinatorLayout> 

and <RelativeLayout> completely deleted. Therefore, instead of using </android.support.design.widget.CoordinatorLayout> only for FAB, we use it for all activity. Since CoordinatorLayout focuses on coordinating the work of children, aren't all the elements working better in coordination with each other? What are the advantages and disadvantages of this path?

+7
android android-layout xml material-design
source share
1 answer

CoordinatorLayout is relatively new, and it solves many problems with FloatingActionButton and NavDrawer. Therefore, if you must have FAB or NavDrawer, I would definitely recommend using CoordinatorLayout as the top-level layout from your last code snippet and inside it, you can have RelativeLayout or LinearLayout for more refinement.

+3
source share

All Articles