【问题标题】:CoordinatorLayout with Toolbar and fragment带有工具栏和片段的 CoordinatorLayout
【发布时间】:2015-10-09 17:02:25
【问题描述】:

我正在使用下面的布局,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>

【问题讨论】:

  • 你能举一个更好的例子吗? RelativeLayout 没有做任何事情,并且在技术上具有 0 的高度,因为 wrap_content 除非您以编程方式对其进行处理。如果您要将其用作containter,请使用FrameLayout
  • 我在上面放了一个片段,做一个片段事务并提供RelativeLayout的ID,我也尝试过使用FrameLayout相同的结果。
  • 很明显你从 Chris Banes 的例子中得到了这个:github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/…。注意他的ViewPager 使用match_parent
  • 您是否将插入片段的高度设置为 match_parent?我对您的布局进行了测试,一切似乎都正常。
  • 是的,我的代码就是基于那个例子。当您从 ViewPager 更改为 FrameLayout/RelativeLayout 时,问题就开始了。 match_parent 对问题没有影响。

标签: android android-fragments android-design-library android-coordinatorlayout android-appbarlayout


【解决方案1】:

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

In this image it shows the Fragment is overlapped by Toolbar.

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

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

The final image.

【讨论】:

【解决方案2】:

如果片段中有 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">

【讨论】:

  • LinearLayout 怎么样?我也有类似的问题。
  • 你能把它包装在 NestedScrollView 中吗?
【解决方案3】:

有同样的问题。使用参数app:layout_behavior="@string/appbar_scrolling_view_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"/>

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多