繁体   English   中英

CoordinatorLayout与工具栏和片段

[英]CoordinatorLayout with Toolbar and fragment

我正在使用下面的布局, CoordinatorLayout在其中AppBarLayout (其中包含ToolbarTabLayout )和占位符RelativeLayout ,因此我可以在其上添加和替换片段。

我遇到了保证金错误,我在RelativeLayout上添加的片段总是超出屏幕底部(类似于AppBarLayout高度的大小),我尝试将它的高度设置为wrap_contentmatch_parent ,在这两种情况下,它都过分了。

如果我从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>

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

在此图像中,它显示片段与工具栏重叠。

现在只需将你的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>

现在你的问题解决了。 它会是这样的。

最终的形象。

如果片段中有ScrollView,您还会看到此问题。 因此,请确保使用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">

有同样的问题。 使用参数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"/>

通过更新recyclerview库解决了这个问题(到com.android.support:recyclerview-v7:22.2.0)

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM