简体   繁体   English

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

[英]Giant white space in between Toolbar and ViewPager while using the Android Design Support Library

I started using the Android Design Support Library in my app. 我开始在我的应用中使用Android设计支持库。 I started using the CoordinatorLayout with an AppBarLayout and Toolbar with a ViewPager to create the animation where the Toolbar leaves the screen on scroll. 我开始将CoordinatorLayout与AppBarLayout和Toolbar与ViewPager一起使用,以创建动画,其中Toolbar使屏幕滚动显示。

So far this works fine. 到目前为止,这很好。 The part that's the problem is whenever the orientation is changed to landscape from portrait, and then back to portrait from landscape. 问题所在是方向从纵向更改为横向,然后从横向更改为纵向。 The result of this is a large white space in between the Toolbar and the ViewPager. 这样的结果是在工具栏和ViewPager之间留有很大的空白。 Does anyone know how to fix this? 有谁知道如何解决这一问题? Thanks! 谢谢! The code is below and I would've added screenshots of the problem, but apparently I can't do so until I reach level 10 or something. 代码在下面,我将添加问题的屏幕截图,但是显然,直到达到10级或类似水平,我才能这样做。

Here's the layout file (activity_main.xml): 这是布局文件(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>

I believe the content holder, your ViewPager, belongs inside the CoordinatorLayout. 我相信内容持有者ViewPager属于CoordinatorLayout内部。 The only thing that I see outside CoordinatorLayout is the nav view or drawer. 我在CoordinatorLayout之外唯一看到的是导航视图或抽屉。

While messing around with the layout behavior, I figured out the problem. 在弄乱布局行为的同时,我发现了问题所在。 So I created a custom layout behavior class to handle the height inconstancy: 因此,我创建了一个自定义布局行为类来处理高度不一致性:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM