【发布时间】:2016-01-03 08:28:09
【问题描述】:
我正在尝试从 ViewPager 内的 Fragment 中的 Android 设计支持库中获取浮动操作按钮。我有 4 个选项卡,我只希望在其中一个选项卡中使用 FAB。我的布局如下:
main_layout.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabIndicatorHeight="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
list_fragment_with_fab.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="5dip"
android:background="@color/Transparent_White"
android:fitsSystemWindows="false"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.lucasr.twowayview.widget.TwoWayView
android:id="@+id/clip_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:twowayview_layoutManager="ListLayoutManager" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_list_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_list_add"
app:layout_anchor="@+id/clip_recycler_view"
app:layout_anchorGravity="bottom|right|end" />
现在的问题是,FAB 不能按照设计规范工作。即工厂的隐藏和显示不起作用。此外,当片段被激活时,FAB 不在其初始位置。为了更清楚,我在下面附上了屏幕截图。
在左图中,如您所见,FAB 不在屏幕上。当我滚动时,工具栏将隐藏(想想 Play 商店应用程序)并且标签仍然存在,那时 FAB 将向上滚动。
这是设计支持库中的错误吗?还是我的布局不正确?另外,我只希望 FAB 出现在其中一个片段中,因此添加 main_layout.xml 有点违背了这个目的。
【问题讨论】:
-
您可以在布局中将可见性设置为
gone,并在片段中设置fab.setVisibility(View.VISIBLE)以显示fab
标签: android android-fragments android-viewpager floating-action-button android-coordinatorlayout