我正在尝试使用CoordinatorLayout
与BottomNavigationView
、AppBarLayout
和ViewPager
。这是我的布局:
<?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:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="enterAlways|scroll"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<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.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemIconTint="?colorPrimaryDark"
app:itemTextColor="?colorPrimaryDark"
app:menu="@menu/navigation"/>
</android.support.design.widget.CoordinatorLayout>
问题在于
CoordinatorLayout
将ViewPager
放置在屏幕底部,导致BottomNavigationView
遮挡了底部,如下所示:
即使CoordinatorLayout
本身没有延伸那么远,这种情况仍然会发生:
我尝试将app:layout_insetEdge="bottom"
添加到BottomNavigationView
,并将app:layout_dodgeInsetEdges="bottom"
添加到ViewPager
,但这会导致另一个问题:它将ViewPager
的底部向上移动,但保持相同的高度,因此顶部被削掉了:
BottomNavigationView
从CoordinatorLayout
中删除,并使它们成为垂直的LinearLayout
中的同级元素。其次,我将ViewPager
和BottomNavigationView
放在一个LinearLayout
下,希望它们能正确布局。但两者都没有起到作用:在第一种情况下,CoordinatorLayout
仍然将ViewPager
的大小与整个屏幕相关联,而要么隐藏部分ViewPager
在BottomNavigationView
后面,要么截掉顶部。在第二种情况下,用户需要滚动才能看到BottomNavigationView
。如何使布局正确?
附言:当我尝试使用@AnoopSS建议的布局(将
CoordinatorLayout
和BottomNavigationView
作为同级元素放在一个RelativeLayout
下)时,我得到了以下结果(ViewPager
仍然向下延伸到BottomNavigationView
后面):
与之前一样,CoordinatorView
本身仅延伸到 BottomNavigationView
的顶部。