协调布局重叠问题

10

请看下面的布局。你会发现,浮动按钮离底部太远了。这是因为工具栏和选项卡都显示出来,而ViewPager的高度是错误的。所以我的问题应该出在layout_height上。但是如何解决呢?

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

floating button too low

以下是布局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,可滑动所有详细条目(因此不需要选项卡)。再次提醒,内部视图似乎太长了(与ToolBar高度相同)。

enter image description here


我并没有说这有什么问题,我是在尝试帮助并探索所有可能的解决方案。在你知道解决方案之前,你永远不会真正知道问题所在,这也是你在这里的原因。如果你认为那不是问题,那就去做,然后你就会知道了。 - doubleA
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - doubleA
1
我添加了一张图片,如果另一个视图有相同的问题但不是FAB。 - Matthias
问题在于viewpager的宽度设置为match_parent。你找到任何解决方案了吗? - Nick
不好意思,我找不到任何解决方案。这两种方法都没有起作用。最终我决定不使用CoordinatorLayout。 - Matthias
显示剩余5条评论
2个回答

3
  1. 你应该使用android.support.v7.widget.RecyclerView而不是ListView。
  2. 虽然你已经在使用android.support.v7.widget.RecyclerView,但一定要确保在你的build.gradle依赖中声明了compile 'com.android.support:recyclerview-v7:23.0.0'。我遇到了相同的问题,即viewpager重叠系统按钮。我通过简单地添加此依赖项来解决它。

1
RecyclerView 真是个超级有 bug 的东西。 - Matthias
1
CoordinatorLayout 无法与 ListView 兼容。在我的项目中,我一直使用 RecyclerView,除非使用不当,否则很少遇到错误。 - Mark Pazon

2
任何你想要“协调”的内容都需要成为CoordinatorLayout直接子元素,包括AppBarRecyclerView(API21+中的ListView或其他支持嵌套滚动的视图也可以),或者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,可以演示设计库。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接