简体   繁体   English

如何在ViewPager和CoordinatorLayout中修复视图的位置

[英]How to fix the position of a view inside a ViewPager and CoordinatorLayout

I have an activity with the layout of: 我的活动布局为:

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

The fragment loaded in ViewPager has a layout of: 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> 

How can I fix the position (prevent scrolling) of the FloatingActionButton inside the fragment (it scrolls with its parent)? 如何修复片段内的FloatingActionButton的位置(防止滚动)(它与其父级滚动)?

Note that I couldn't move FloatingActionButton to the activity layout. 请注意,我无法将FloatingActionButton移动到活动布局。

You should move your floating action button from your view pager to coordinator layout. 您应该将浮动操作按钮从视图寻呼机移动到协调器布局。 From there you can set your floating action button position with 从那里你可以设置你的浮动动作按钮位置

android:layout_gravity="end|bottom"

or 要么

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

And don't forget to put your floating action button on the lowermost of your coordinator layout XML to ensure that your floating action button at the top of your layout. 并且不要忘记将您的浮动操作按钮放在协调器布局XML的最下方,以确保您的布局顶部的浮动操作按钮。

You should use a FAB in activity layout. 您应该在活动布局中使用FAB。 .If you need different colors or actions you can define FAB in your fragment and set the color or the action for each fragment. 。如果您需要不同的颜色或动作,您可以在片段中定义FAB并为每个片段设置颜色或动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CoordinatorLayout中的Viewpager无法滚动 - Viewpager inside CoordinatorLayout not scrolling 避免在 CoordinatorLayout 内的 ViewPager 内的底部视图中滚动 - Avoid scrolling in bottom view inside ViewPager inside CoordinatorLayout CoordinatorLayout内ViewPager内的CoordinatorLayout滚动问题 - CoordinatorLayout inside ViewPager inside CoordinatorLayout scrolling issue 如何正确修复 CoordinatorLayout 内的页脚 - How to fix footer properly which is inside CoordinatorLayout Tablayout覆盖了CoordinatorLayout内部的viewpager - Tablayout is covering the viewpager inside the CoordinatorLayout 在CoordinatorLayout中滚动Viewpager时的静态视图 - Static view in scrolling Viewpager in CoordinatorLayout CoordinatorLayout中的ViewPager中的非滚动片段 - Non-Scrolling Fragment in a ViewPager inside CoordinatorLayout CoordinatorLayout内的Viewpager不滚动“折叠”工具栏 - Viewpager inside CoordinatorLayout not scrolling the Collapsing toolbar ViewPager显示CoordinatorLayout内部的滚动问题 - ViewPager showing Scrolling issue inside CoordinatorLayout Android:CoordinatorLayout-如何在顶部屏幕显示修复标题视图? - Android : CoordinatorLayout - How to have a fix header view at the top screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM