【发布时间】:2015-10-09 17:02:25
【问题描述】:
我正在使用下面的布局,CoordinatorLayout 包含在其中 AppBarLayout(其中包含 Toolbar 和 TabLayout)和一个占位符 RelativeLayout,因此我可以在其上添加和替换片段。
我遇到边距错误,我在 RelativeLayout 上添加的片段总是会超出屏幕底部(与AppBarLayout 高度的大小相似),我尝试设置它的高度到wrap_content 和match_parent,在这两种情况下都过火了。
如果我从RelativeLayout 中删除app:layout_behavior="@string/appbar_scrolling_view_behavior",它的顶部将位于AppBarLayout 下方,这也不是预期的结果。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: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"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabIndicatorHeight="4dp"
app:tabIndicatorColor="#ffffff"
app:tabMode="scrollable"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="20dp"
android:src="@drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
你能举一个更好的例子吗?
RelativeLayout没有做任何事情,并且在技术上具有0的高度,因为wrap_content除非您以编程方式对其进行处理。如果您要将其用作containter,请使用FrameLayout。 -
我在上面放了一个片段,做一个片段事务并提供RelativeLayout的ID,我也尝试过使用FrameLayout相同的结果。
-
很明显你从 Chris Banes 的例子中得到了这个:github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/…。注意他的
ViewPager使用match_parent? -
您是否将插入片段的高度设置为 match_parent?我对您的布局进行了测试,一切似乎都正常。
-
是的,我的代码就是基于那个例子。当您从 ViewPager 更改为 FrameLayout/RelativeLayout 时,问题就开始了。 match_parent 对问题没有影响。
标签: android android-fragments android-design-library android-coordinatorlayout android-appbarlayout