[英]CoordinatorLayout with Toolbar and fragment
I'm using the layout below, The CoordinatorLayout
holds inside it AppBarLayout
(With Toolbar
and TabLayout
inside it) and a placeholder RelativeLayout
, so I could add and replace fragments on it. 我正在使用下面的布局, CoordinatorLayout
在其中AppBarLayout
(其中包含Toolbar
和TabLayout
)和占位符RelativeLayout
,因此我可以在其上添加和替换片段。
I'm experiencing margin errors, the fragments I add on the RelativeLayout will always over expand beyond the bottom of the screen (in the amount similar to the size of the AppBarLayout
height), I've tried setting it's height to wrap_content
and match_parent
, in both cases it goes overboard. 我遇到了保证金错误,我在RelativeLayout上添加的片段总是超出屏幕底部(类似于AppBarLayout
高度的大小),我尝试将它的高度设置为wrap_content
和match_parent
,在这两种情况下,它都过分了。
if I remove the app:layout_behavior="@string/appbar_scrolling_view_behavior"
from the RelativeLayout
the top of it will be under the AppBarLayout
which is also not the desired outcome. 如果我从RelativeLayout
移除app:layout_behavior="@string/appbar_scrolling_view_behavior"
,它的顶部将位于AppBarLayout
下,这也不是所需的结果。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<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="4dp"
app:tabIndicatorColor="#ffffff"
app:tabMode="scrollable"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="20dp"
android:src="@drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>
I have figured out the problem on showing fragment below toolbar when using the coordinator layout. 在使用协调器布局时,我已经找到了在工具栏下方显示片段的问题。 The problem in my case is: 我的问题是:
In this image it shows the Fragment is overlapped by Toolbar. 在此图像中,它显示片段与工具栏重叠。
Now just put your AppBarLayout and FrameLayout inside LinearLayout like below, 现在只需将你的AppBarLayout和FrameLayout放在LinearLayout中,如下所示,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/mainappbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Now your problem is solved. 现在你的问题解决了。 And it will be like this. 它会是这样的。
You will also see this issue if you have a ScrollView inside the fragment. 如果片段中有ScrollView,您还会看到此问题。 So make sure you use a NestedScrollView instead: 因此,请确保使用NestedScrollView:
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
Had the same issue. 有同样的问题。 Changing RelativeLayout
to FrameLayout
with parameter app:layout_behavior="@string/appbar_scrolling_view_behavior"
solved my problem. 使用参数app:layout_behavior="@string/appbar_scrolling_view_behavior"
将RelativeLayout
更改为FrameLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
解决了我的问题。
<FrameLayout
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
The issue has been solved by updating the recyclerview library (to com.android.support:recyclerview-v7:22.2.0) 通过更新recyclerview库解决了这个问题(到com.android.support:recyclerview-v7:22.2.0)
The fragment I was loading had a recyclerview in it. 我加载的片段中有一个recyclerview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.