android - 如何固定 View 在 ViewPager 和 CoordinatorLayout 中的位置

标签 android android-layout android-design-library android-coordinatorlayout coordinator-layout

我有一个布局为:

<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 中加载的 fragment 具有以下布局:

<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> 

如何固定 fragment 内 FloatingActionButton 的位置(防止滚动)(它随父元素一起滚动)?

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

最佳答案

您应该将 float 操作按钮从 View 寻呼机移至协调器布局。从那里您可以使用

设置 float 操作按钮的位置
android:layout_gravity="end|bottom"

app:layout_anchor="@id/yourViewToAnchor"
app:layout_anchorGravity="bottom|right|end"

并且不要忘记将 float 操作按钮放在协调器布局 XML 的最下方,以确保 float 操作按钮位于布局的顶部。

关于android - 如何固定 View 在 ViewPager 和 CoordinatorLayout 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808437/

相关文章:

android - Google Play 服务 - API 版本 6 太高

java - 时间选择器对话框 : Change hours/minutes text size

Android - ConstraintLayout 中的 ConstraintVertical 权重不起作用?

android - NavigationView 从右侧滑动

android - 如何禁用 NestedScrollView&CollapsingToolbarLayout 的滚动,例如当下面没有更多内容时?

Android 抽屉导航布局

android - WebRTC识别远程流视频停止

java - Android如何检测网络类型改变事件?

java - 应用程序在小屏幕上崩溃但在大屏幕上不会

java - 向 Android 应用程序添加依赖项后出错