CoordinatorLayout 重叠

CoordinatorLayout overlapping

看看下面的布局。您会看到,浮动按钮距离底部太远。这是因为显示了 Toolbar 和 Tabs,并且 ViewPager 的高度不正确。所以不知何故我在 layout_height 上做错了什么。但是我该如何解决呢?

备注:ViewPager 是主要内容,它在第二个选项卡中包含带有 ListView 和 Google Map V2 的片段。

这就是布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        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:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
            android:id="@+id/pager_list_views"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="fill_parent">
    </android.support.v4.view.ViewPager>

</android.support.design.widget.CoordinatorLayout>

这是第一个选项卡(列表)中片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ListView
              android:id="@+id/preview_list"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:choiceMode="singleChoice"
              android:orientation="vertical" />

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/action_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="16dp"
            android:src="@mipmap/ic_add_white_48dp" />

</android.support.design.widget.CoordinatorLayout>

只是为了确定; FAB 没有问题。看这张照片。这是一个类似的布局。一个 CoordinatorLayout 带有一个 ToolBar 和一个 ViewPager,它可以浏览所有的细节条目(因此不需要标签)。再一次,内部视图似乎太长了(与工具栏的高度相同)。

  1. 您应该使用 android.support.v7.widget.RecyclerView 而不是 ListView
  2. 尽管您已经在使用 android.support.v7.widget.RecyclerView,但请 100% 确定您在 build.gradle 依赖项中声明了 compile 'com.android.support:recyclerview-v7:23.0.0'。我遇到了 viewpager 与系统按钮重叠的问题。我通过简单地添加此依赖项来修复它。

任何你想要"coordinate"的东西都需要直接child of CoordinatorLayout,包括AppBar,RecyclerView(API21+中的ListView或其他支持嵌套滚动的view即可),或FAB

您的 FAB 偏移到屏幕外的原因是:

  1. ViewPager 有一个 @string/appbar_scrolling_view_behavior,此行为的实现将在您滚动时偏移视图。
  2. 你把 FAB 放在 ViewPager 里。

所以当ViewPager的偏移量改变时,ViewPager内的任何东西都会一起偏移(额外的CoordinatorLayout对改变偏移量没有帮助)。

要解决此问题,请不要在 ViewPager 之外使用 CoordinatorLayout

或:

  1. 将您的 FAB 放在 ViewPager 之外,这样它就不会随着 ViewPager 滚动。
  2. 如果 FAB 仅适用于您的部分页面,hide()在需要时使用它。

顺便说一句,有很好的应用程序,Cheesesquare Sample,可以演示设计库。