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?
我对android的材料设计方面比较陌生。所以,请容忍我。我尝试为我的家庭活动实现FAB,然后实现了snackbar。很自然,它出现在我的FAB上面。我研究了CoordinatorLayout(协调器布局),让我感到困扰的是,使用CoordinatorLayout(协调器布局)的一个更好的做法是什么?
For example, here's the xml of my activity.
例如,这是我的活动的xml。
<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.
我看到其他人像这样添加CoordinatorLayout。
<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>
.
我的问题是,我们能否使用< android.support.designwidget.coordinatorlayout >作为所有元素的根。完全绕过或删除
So the xml becomes like this.
xml是这样的。
<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?
并且
1 个解决方案
#1
3
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.
CoordinatorLayout相对较新的,它可以解决很多问题,比如FloatingActionButton和NavDrawer。因此,如果您必须拥有FAB或NavDrawer,我肯定会建议您从现在开始使用CoordinatorLayout作为您的顶层布局(您的上一个代码片段),并且在其中可以使用RelativeLayout或LinearLayout来进行更精细的设计。
#1
3
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.
CoordinatorLayout相对较新的,它可以解决很多问题,比如FloatingActionButton和NavDrawer。因此,如果您必须拥有FAB或NavDrawer,我肯定会建议您从现在开始使用CoordinatorLayout作为您的顶层布局(您的上一个代码片段),并且在其中可以使用RelativeLayout或LinearLayout来进行更精细的设计。