ViewPager与AppBarLayout重叠
问题描述:
我试图设置Activity
的布局,其中包含N Fragments
。在布局我用一个ViewPager
和AppBarLayout
,我所面临的问题是,ViewPager
重叠AppBarLayout
,你从截图中可以看到:ViewPager与AppBarLayout重叠
这是我activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/containter">
</android.support.v4.view.ViewPager>
这是我app_bar_main.xml
其中包含AppBarLayout
<?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"
tools:context="com.app.app.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
如何防止这些元素重叠?
答
@Signo,
您的问题可以通过一些布局调整来解决。
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/containter" />
</android.support.v4.view.DrawerLayout>
</LinearLayout>
答
首先的尝试添加上述你的观点有点像一个ViewGroup:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//... your views here
</RelativeLayout>
,你应该你appBar的改变高度wrap_content
并添加一些ID
<include
android:id="@+id/app_bar_id"
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
然后在您的视图寻呼机上定义了像这样的layout_below属性:
<android.support.v4.view.ViewPager
android:id="@+id/containter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/app_bar_id"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
谢谢:)我试过,它几乎工作了,我不得不把'机器人变化:'include'元素的layout_height'到'wrap_content'现在重叠走了 – Signo
这很好,如果你编辑你的问题来添加你的解决方案将是惊人的:) @Signo –
如果你不介意我已经编辑你的答案,所以我可以接受它:)再次,谢谢! – Signo