我目前在使用Android design-support库中的CoordinatorLayout
中的FrameLayout
遇到了问题,而我是按照这个文章创建选项卡的指示进行操作的。
基本上大部分功能都正常工作,容器片段被膨胀到FrameLayout
中,并且它们的选项卡片段正确地添加到ViewPager
作为选项卡(需要这样做,因为我有许多片段应该重用布局)。
我正在努力解决的问题是FrameLayout
(以及选项卡片段)会占用整个屏幕高度,因此会重叠Toolbar
和TabLayout
。为了可视化问题,我创建了以下图像:
具有CoordinatorLayout
、Toolbar
和TabLayout
的基本布局:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
用于填充到container
的片段所使用的单独布局:
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
所有片段都由我的 BaseFragment
类膨胀(在另一个SO帖子中,调用 inflater.inflate(getLayoutRes(), null);
是导致相同问题的原因)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getLayoutRes(), container, false);
}
如果我用普通的LinearLayout
替换CoordinatorLayout
,那么FrameLayout
会按预期从AppBarLayout
下方开始,但根据文档,AppBarLayout
的大多数功能需要直接作为CoordinatorLayout
的子级。我可以给
FrameLayout
添加marginTop
,但我想知道是否有适当的解决方法。感谢提供任何提示!
appbar_scrolling_view_behavior
到底是做什么的呢..? - MicroAppBarLayout
重叠,并允许AppBarLayout
响应滚动,如果放在可滚动视图上(例如NestedScrollView
或RecyclerView
)。 - ianhanniballake