简体   繁体   English

CoordinatorLayout与工具栏和片段

[英]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 (其中包含ToolbarTabLayout )和占位符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_contentmatch_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. 它会是这样的。

The final image. 最终的形象。

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.

相关问题 使用CoordinatorLayout隐藏工具栏,但在片段上使用RecyclerView - Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment 使用CoordinatorLayout从片段中隐藏活动中的工具栏 - Hide Toolbar in activity from fragment using CoordinatorLayout Android - Fragment中CoordinatorLayout状态栏下的工具栏 - Android - Toolbar under status bar in CoordinatorLayout in Fragment 来自 Fragment 的 Activity 中的 CoordinatorLayout 折叠工具栏 - CoordinatorLayout collapse Toolbar in Activity from Fragment 半透明/透明状态栏 + CoordinatorLayout + Toolbar + Fragment - Translucent/Transparent status bar + CoordinatorLayout + Toolbar + Fragment 仅在特定片段中滚动时,CoordinatorLayout 隐藏工具栏 - CoordinatorLayout hide toolbar when scrolling only in specific fragment 使用CoordinatorLayout、Toolbar和Fragment在Fragment之间切换时如何让工具栏出现 - How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment 工具栏不响应CoordinatorLayout中的滚动 - Toolbar not responding to scroll in CoordinatorLayout 工具栏在CoordinatorLayout中不会在滚动时折叠 - Toolbar not collapsing on scroll in CoordinatorLayout 工具栏在CoordinatorLayout中向下滚动 - Toolbar scrolls down in CoordinatorLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM