如果我想要一个 ViewPager
有几个Fragments
, 和一个 FloatingActionButton
被其中之一使用 Fragments
,什么是理想的布局行为?
一个。应该FAB
位于 Activity
布局,其中还包含 CoordinatorLayout
拿着AppBarLayout
/Toolbar
/ViewPager
?
或
B.应该FAB
位于 Fragment
布局?
选项 A 的意义在于 FAB
应该能够根据需要在 CoordinatorLayout
内制作动画.例如,如果我显示 Snackbar
, 我想要 FAB
随着 Snackbar
的显示/隐藏向上/向下移动.但是,放置 FAB
这里需要我隐藏/显示 FAB
必要时,因为另外两个 Fragments
不会使用 FAB
并且应该将其隐藏起来。
选项 B 本质上是这些好处/缺点的反面。
哪个更正确?
谢谢, 扎克
最佳答案
A. Should the FAB be located in the
Activity
layout, which also contains theCoordinatorLayout
holding theAppBarLayout/Toolbar/ViewPager
?
是的,完全正确。
看这个例子:
<CoordinatorLayout>
<AppbarLayout/>
<scrollableView/>
<FloatingActionButton/>
</CoordinatorLayout>
如您所见,在上面的示例中,我们在 CoordinatorLayout
中使用了 FloatingActionButton
,FloatingActionButton
可以一直在布局中滚动 Fragment
或任何 View 后无需滚动甚至隐藏。
最后,您可以将其用于您的 Fragment
:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/dashboard_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.Toolbar"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/dashboard_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
另外,让我提一下,我已经添加了 FrameLayout
和 app:layout_behavior="@string/appbar_scrolling_view_behavior"
它可以在 AppbarLayout
下。
这就是该行的重点。
B. Should the FAB be located in the
Fragment
layout?
这取决于你,但是,将它与 CoordinatorLayout
一起使用是最好的方法。我的意思是,你可以用它实现许多 MaterialDesign 的东西。
关于android - ViewPager 和 FAB 的正确布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912552/