android: Which practice is better for using CoordinatorLayout

I am new to material design aspect of android. So, please bear with me. I tried implementing the FAB for my home activity, and then implemented the snackbar. Naturally, it came above my FAB. I researched on CoordinatorLayout and what's bothering me is which one is a better practice for using a CoordinatorLayout?

For example, here's 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 adding 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 the <RelativeLayout> .

So the xml becomes 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 the <RelativeLayout> is completely removed. So instead of using </android.support.design.widget.CoordinatorLayout> just for FAB, we use it for the whole activity. As CoordinatorLayout focuses on making child views work coordinated, isn't it better if all elements work in coordination with one another? What are the advantages and disadvantages of doing it either ways?


CoordinatorLayout is relatively new and it resolves a lot of issues with FloatingActionButton and NavDrawer. So if you must have FAB or NavDrawer I would definitely recommend using CoordinatorLayout as your top level layout from now on (your last code snippet) and inside it you can have RelativeLayout or LinearLayout for more refinement.

链接地址: http://www.djcxy.com/p/29730.html

上一篇: 科尔多瓦

下一篇: android:哪种做法更适合使用CoordinatorLayout