10

看看下面的布局。您会看到,浮动按钮离底部太远了。这是因为显示了 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的问题。看这张照片。这是一个类似的布局。带有 ToolBar 和 ViewPager 的 CoordinatorLayout,它可以扫过所有详细信息条目(因此不需要选项卡)。再一次,内部视图似乎太长(与工具栏高度相同)。

在此处输入图像描述

4

2 回答 2

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

任何你要“协调”的东西都需要是的直接子级CoordinatorLayout,包括AppBar, RecyclerView(API21+ 中的 ListView 或其他支持嵌套滚动的视图都可以),或者FAB,等等。

你的屏幕偏移的原因FAB是:

  1. ViewPager有一个@string/appbar_scrolling_view_behavior,当你滚动时,这个行为的实现会偏移视图。
  2. 你放在FAB里面ViewPager

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

要解决此问题,请不要使用CoordinatorLayoutoutside ViewPager

或者:

  1. 把你放在FAB外面,ViewPager这样它就不会滚动了ViewPager
  2. 如果 FAB 仅适用于您的某些页面,hide()则在需要时使用。

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

于 2016-06-05T10:04:04.390 回答