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

Java 使用Android设计支持库时,工具栏和ViewPager之间存在巨大的空白,java,android,xml,Java,Android,Xml,我开始在我的应用程序中使用Android设计支持库。我开始使用带有AppBarLayout的CoordinatorLayout和带有ViewPager的工具栏来创建工具栏滚动离开屏幕的动画 到目前为止,这一切都很好。问题在于,当方向从纵向更改为横向,然后从横向更改为纵向时。其结果是工具栏和ViewPager之间有一个很大的空白。有人知道如何解决这个问题吗?谢谢下面是代码,我会添加问题的屏幕截图,但显然,在我达到10级或其他级别之前,我不能这么做 以下是布局文件(activity_main.xml

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

到目前为止,这一切都很好。问题在于,当方向从纵向更改为横向,然后从横向更改为纵向时。其结果是工具栏和ViewPager之间有一个很大的空白。有人知道如何解决这个问题吗?谢谢下面是代码,我会添加问题的屏幕截图,但显然,在我达到10级或其他级别之前,我不能这么做

以下是布局文件(activity_main.xml):



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

我相信内容持有者,您的ViewPager,属于CoordinatorLayout内部。我在Coordinator布局外部看到的唯一东西是导航视图或抽屉。

在处理布局行为时,我发现了问题所在。因此,我创建了一个自定义布局行为类来处理高度变化:

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;
    }
}
导入android.content.Context;
导入android.support.design.widget.AppBarLayout;
导入android.support.design.widget.CoordinatorLayout;
导入android.support.v4.view.ViewCompat;
导入android.util.AttributeSet;
导入android.view.view;
导入java.util.List;
公共类CustomBehavior扩展了AppBarLayout.ScrollingViewBehavior
{
公众行为
{
超级();
}
公共自定义行为(上下文、属性集属性)
{
超级(上下文,attrs);
}
@凌驾
MeasureChild上的公共布尔值(坐标布局父对象、视图子对象、int-parentWidthMeasureSpec、int-widthUsed、int-parentHeightMeasureSpec、int-heightUsed)
{
if(child.getLayoutParams().height==-1)
{
List dependencies=parent.getDependencies(child);
if(dependencies.isEmpty())
{
返回false;
}
AppBarLayout appBar=getOriginalLayout(依赖项);
if(appBar!=null&&viewcompt.isLaidOut(appBar))
{
if(ViewCompat.getFitsSystemWindows(appBar))
{
ViewCompat.setFitsSystemWindows(子级,true);
}
int scrollRange=appBar.getTotalScrollRange();
int parentHeight=View.MeasureSpec.getSize(parentHeight-MeasureSpec);
int height=parentHeight-appBar.getMeasuredHeight()+scrollRange;
int heightmeasurept=View.MeasureSpec.makeMeasureSpec(高度,View.MeasureSpec.AT_);
父对象。onMeasureChild(子对象,父对象宽度测量等级,宽度使用,高度测量等级,高度使用);
返回true;
}
}
返回false;
}
私有静态AppBarLayout getOriginalLayout(列表视图)
{
int i=0;
对于(int z=views.size();i
在处理布局行为时,我发现了问题所在。因此,我创建了一个自定义布局行为类来处理高度变化:

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;
    }
}
导入android.content.Context;
导入android.support.design.widget.AppBarLayout;
导入android.support.design.widget.CoordinatorLayout;
导入android.support.v4.view.ViewCompat;
导入android.util.AttributeSet;
导入android.view.view;
导入java.util.List;
公共类CustomBehavior扩展了AppBarLayout.ScrollingViewBehavior
{
公众行为
{
超级();
}
公共自定义行为(上下文、属性集属性)
{
超级(上下文,attrs);
}
@凌驾
MeasureChild上的公共布尔值(坐标布局父对象、视图子对象、int-parentWidthMeasureSpec、int-widthUsed、int-parentHeightMeasureSpec、int-heightUsed)
{
if(child.getLayoutParams().height==-1)
{
List dependencies=parent.getDependencies(child);
if(dependencies.isEmpty())
{
返回false;
}
AppBarLayout appBar=getOriginalLayout(依赖项);
if(appBar!=null&&viewcompt.isLaidOut(appBar))
{
if(ViewCompat.getFitsSystemWindows(appBar))
{
ViewCompat.setFitsSystemWindows(子级,true);
}
int scrollRange=appBar.getTotalScrollRange();
int parentHeight=View.MeasureSpec.getSize(parentHeight-MeasureSpec);
int height=parentHeight-appBar.getMeasuredHeight()+scrollRange;
int heightmeasurept=View.MeasureSpec.makeMeasureSpec(高度,View.MeasureSpec.AT_);
父对象。onMeasureChild(子对象,父对象宽度测量等级,宽度使用,高度测量等级,高度使用);
返回true;
}
}
返回false;
}
私有静态AppBarLayout getOriginalLayout(列表视图)
{
int i=0;
对于(int z=views.size();i
感谢您的回复!但是ViewPager在Coordinator布局中。我还更新了代码,这样你就可以看到整个布局文件。谢谢你的回复!但是ViewPager在Coordinator布局中。我也是upda