android - ViewPager 和 FAB 的正确布局?

标签 android android-fragments android-viewpager android-coordinatorlayout floating-action-button

如果我想要一个 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 the CoordinatorLayout holding the AppBarLayout/Toolbar/ViewPager?

是的,完全正确。

看这个例子:

<CoordinatorLayout>
    <AppbarLayout/>
    <scrollableView/>
    <FloatingActionButton/>
</CoordinatorLayout>

如您所见,在上面的示例中,我们在 CoordinatorLayout 中使用了 FloatingActionButtonFloatingActionButton 可以一直在布局中滚动 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>

另外,让我提一下,我已经添加了 FrameLayoutapp: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/

相关文章:

android - 如何在 fragment 中保存状态

java - 我的应用程序在横向 View 中困惑

android - 尝试在空对象引用上调用虚拟方法 'void android.graphics.Bitmap.copyPixelsFromBuffer(java.nio.Buffer)'

android - 在 ViewPager 中动态添加和删除 fragment

Android:静态水平进度条可以嵌入到Textview中吗?

android - Android 中的单元测试私有(private)函数

android - ViewPager 中光标驱动的内容

android - 使用 FragmentPagerAdapter 在 android ViewPager 中滑动不工作

android - 在 ListView 中滑动手势 - Android

Android Studio 不创建新项目