Fil*_*ito 5 android android-layout
我有一个简单的布局,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text: "NOT CENTRALIZED!" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />   
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
上面的布局属于一个 Fragment,它被加载到一个 ViewPager(它属于一个 Activity——这里称为 ActivityA.java)。
ActivityA.java 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"
xmlns:tools="http://schemas.android.com/tools"
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:stateListAnimator="@animator/elevated_appbar"
    app: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"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:tabIndicatorColor="@android:color/white"
            app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
请注意,我正在使用CoordinatorLayout并且我的ViewPager具有该 
app:layout_behavior="@string/appbar_scrolling_view_behavior"属性。
这让我困惑的小东西:为什么我的TextView不上,因为我声明了布局集中android:layout_centerVertical="true",并android:layout_centerVertical="true"给它?
奇怪的是,当我删除android.support.v4.view.ViewPagerapp:layout_scrollFlags="scroll|enterAlways"上的属性时,内容显示在屏幕中间,但是...... android.support.v7.widget.Toolbar停止折叠和展开功能。
编辑
在布局预览中,我们可以看到android.support.v4.view.ViewPager不在导航栏上方(但app:layout_behavior="@string/appbar_scrolling_view_behavior"需要将此视图放在 下方android.support.design.widget.AppBarLayout)。删除此属性时,android.support.v4.view.ViewPager会上升导航栏,但与android.support.design.widget.AppBarLayout.
看一看:
与app:layout_behavior="@string/appbar_scrolling_view_behavior":
没有app:layout_behavior="@string/appbar_scrolling_view_behavior":
编辑 2
所以,我决定做一个测试,在一个很好的参考项目上重现这个问题,该项目展示了一些 Material 特性,结果是......
... 同样的问题!
如果有人想自己复现,只需将fragment_cheese_list.xml改为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone" />
<android.support.v7.widget.AppCompatTextView
    android:id="@+id/alertMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:text="ALSO, NOT CENTRALIZED..." />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
    尝试添加app:layout_behavior="@string/appbar_scrolling_view_behavior"到 Fragment 的布局 xml 文件中。
Edit1
实际上它的行为正如它应该的那样。当你有你Viewpager的:app:layout_behavior="@string/appbar_scrolling_view_behavior" 
AndroidViewPager根据高度向下移动AppBarLayout(可能会为其添加边距)。当您向上滚动视图时,该边距会重新调整。因为那scrolling_view_behavior就是
并且没有:app:layout_behavior="@string/appbar_scrolling_view_behavior" 
您ViewPager是独立的AppBarLAyout,因此,没有边距,这就是您TextView在这种情况下出现在中心的原因。

因此,实际上,您的 TextView 始终位于 ViewPager 的中心。是 ViewPager 发生了变化。要更改该行为,您可能需要实现自己的布局行为。安卓就是这样做的。