Android 与工具栏和片段协调布局

Android 与工具栏和片段协调布局,android,android-fragments,android-design-library,android-coordinatorlayout,android-appbarlayout,Android,Android Fragments,Android Design Library,Android Coordinatorlayout,Android Appbarlayout,我正在使用下面的布局,CoordinatorLayout保存在里面AppBarLayout(里面有工具栏和表格布局)和一个占位符RelativeLayout,因此我可以在上面添加和替换片段 我遇到了边距错误,我在RelativeLayout上添加的片段总是会超出屏幕底部(数量类似于AppBarLayoutheight的大小),我尝试将其高度设置为wrap\u content和match\u parent,在这两种情况下,它都超出了范围 如果我从RelativeLayout中删除app:layou

我正在使用下面的布局,
CoordinatorLayout
保存在里面
AppBarLayout
(里面有
工具栏和
表格布局)和一个占位符
RelativeLayout
,因此我可以在上面添加和替换片段

我遇到了边距错误,我在RelativeLayout上添加的片段总是会超出屏幕底部(数量类似于
AppBarLayout
height的大小),我尝试将其高度设置为
wrap\u content
match\u parent
,在这两种情况下,它都超出了范围

如果我从
RelativeLayout
中删除
app:layout\u behavior=“@string/appbar\u scrolling\u view\u 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>

通过更新recyclerview库(到com.android.support:recyclerview-v7:22.2.0),该问题已得到解决


我加载的片段中有一个recyclerview。

如果片段中有滚动视图,您也会看到这个问题。因此,请确保改用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">

我已经解决了在使用协调器布局时在工具栏下方显示片段的问题。我的问题是:

现在只需将AppBarLayout和FrameLayout放入线性布局中,如下所示

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


也有同样的问题。使用参数
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
RelativeLayout
更改为
FrameLayout
,解决了我的问题

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


你能举个更好的例子吗?由于
wrap\u content
的原因,
RelativeLayout
不起任何作用,并且在技术上高度为
0
,除非您以编程方式使用它。如果要将其用作
容器
,请使用
框架布局
。我将在其上放置一个片段,执行一个片段事务并提供相对物的id,我还尝试使用框架布局获得相同的结果。很明显,您从Chris Banes的示例中得到了这个结果:。注意他的
ViewPager
使用
match\u parent
?您是否将插入片段的高度设置为与父片段匹配?我对你的布局做了一个测试,它似乎都正常工作。是的,我的代码就是基于这个例子。当您从ViewPager更改为FrameLayout/RelativeLayout时,问题就开始了。匹配\u父项对该问题没有影响。线性布局如何?我也遇到了类似的问题。您能将其包装到NestedScrollView中吗?
AppBarLayout
应该是
CoordinatorLayout
的直接子级。见: