使用Android设计支持库时,工具栏和ViewPager之间存在巨大的空白区域

时间:2015-08-17 19:53:40

标签: java android xml

我开始在我的应用中使用Android设计支持库。我开始使用带有ViewPager的AppBarLayout和工具栏的CoordinatorLayout来创建动画,其中工具栏在滚动时离开屏幕。

到目前为止这个工作正常。问题的部分是当方向从纵向变为横向,然后从横向变回纵向时。结果是工具栏和ViewPager之间有一个很大的空白区域。有谁知道如何解决这一问题?谢谢!代码在下面,我会添加问题的截图,但显然我不能这样做,直到我达到10级或其他什么。

这是布局文件(activity_main.xml):

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

<!--Content Container-->
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--App Bar Container-->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--ActionBar-->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <!--Prevents a bug in the App Bar Layout-->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="1px"/>

        <!--Tabs-->
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:tabMode="scrollable"
            app:tabGravity="fill"
            app:tabIndicatorHeight="@dimen/toolbar_tab_indicator_height"
            app:tabTextColor="@color/colorPrimaryLight"
            app:tabSelectedTextColor="@color/textColor"/>
    </android.support.design.widget.AppBarLayout>

    <!--Body Content-->
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>


<!--Navigation Drawer-->
<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/drawer_items"/>
</android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:0)

我相信内容持有者,您的ViewPager,属于CoordinatorLayout。我在CoordinatorLayout外面看到的唯一一件事是导航视图或抽屉。

答案 1 :(得分:0)

在弄乱布局行为时,我发现了问题所在。所以我创建了一个自定义布局行为类来处理高度不稳定性:

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import java.util.List;


public class CustomBehavior extends AppBarLayout.ScrollingViewBehavior
{
    public CustomBehavior()
    {
        super();
    }

    public CustomBehavior(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }


    @Override
    public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)
    {
        if (child.getLayoutParams().height == -1)
        {
            List dependencies = parent.getDependencies(child);

            if (dependencies.isEmpty())
            {
                return false;
            }

            AppBarLayout appBar = getOriginalLayout(dependencies);

            if (appBar != null && ViewCompat.isLaidOut(appBar))
            {
                if (ViewCompat.getFitsSystemWindows(appBar))
                {
                    ViewCompat.setFitsSystemWindows(child, true);
                }

                int scrollRange = appBar.getTotalScrollRange();
                int parentHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec);
                int height = parentHeight - appBar.getMeasuredHeight() + scrollRange;
                int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);

                parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);

                return true;
            }
        }

        return false;
    }


    private static AppBarLayout getOriginalLayout(List<View> views)
    {
        int i = 0;

        for (int z = views.size(); i < z; ++i)
        {
            View view = (View)views.get(i);

            if (view instanceof AppBarLayout)
            {
                return (AppBarLayout)view;
            }
        }

        return null;
    }
}