[英]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.