如何在ViewPager和CoordinatorLayout中固定一个视图的位置

9

我有一个布局为:

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="56dp"
                android:layout_width="match_parent"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="wrap_content"
                app:tabIndicatorColor="@color/indicator"
                app:tabMode="scrollable"
                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" />

</android.support.design.widget.CoordinatorLayout>

ViewPager中加载的片段具有以下布局:
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp"
            android:scrollbars="vertical" />

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/download_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:src="@drawable/ic_btn_download" />

</RelativeLayout> 

如何将浮动操作按钮(FloatingActionButton)在片段内固定位置(防止滚动)?

请注意,我无法将 FloatingActionButton 移动到活动布局中。


为什么你不能将FloatingActionButton移动到你的活动布局中? - Markus Rubey
2个回答

10

你应该将浮动操作按钮从你的视图页面移动到协调布局中。从那里,你可以使用

app:layout_anchor

app:layout_anchorGravity

来设置浮动操作按钮的位置。

android:layout_gravity="end|bottom"
或者
app:layout_anchor="@id/yourViewToAnchor"
app:layout_anchorGravity="bottom|right|end"

别忘了将浮动操作按钮放在协调布局 XML 的最底部,以确保您的浮动操作按钮位于布局顶部。


3

在活动界面中,应使用FAB。如果需要不同的颜色或操作,则可以在您的片段中定义FAB,并为每个片段设置颜色或操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接