【发布时间】:2018-06-03 16:21:53
【问题描述】:
我正在尝试将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 的底部向上移动,但它保持相同的高度,所以顶部现在被切掉了:
我尝试了另外两个实验。首先,我尝试从CoordinatorLayout 中删除BottomNavigationView,并使它们成为垂直LinearLayout 下的兄弟姐妹。其次,我将ViewPager 和BottomNavigationView 放在LinearLayout 下,希望它们的布局正确。两者都没有帮助:在第一种情况下,CoordinatorLayout 仍然相对于整个屏幕调整ViewPager 的大小,要么将部分隐藏在BottomNavigationView 后面,要么切掉顶部。在第二种情况下,用户需要滚动才能看到BottomNavigationView。
如何正确布局?
附:当我尝试the layout suggested by @Anoop S S (将CoordinatorLayout 和BottomNavigationView 作为兄弟姐妹放在RelativeLayout 下)时,我得到以下信息(ViewPager 仍在BottomNavigationView 后面向下延伸):
和以前一样,CoordinatorView 本身仅向下延伸到BottomNavigationView 的顶部。
【问题讨论】:
-
我创建了一个关于这个issuetracker.google.com/issues/116541304的问题
标签: android android-layout bottomnavigationview coordinator-layout