【问题标题】:Android SupportLib - FrameLayout in CoordinatorLayout with AppBarLayout consuming entire screen-heightAndroid SupportLib - CoordinatorLayout 中的 FrameLayout 与 AppBarLayout 占用整个屏幕高度
【发布时间】:2015-09-06 18:51:35
【问题描述】:

我目前在来自 Android 设计支持库的 CoordinatorLayout 中遇到 FrameLayout 的问题,而我在创建选项卡时遵循了此 post 中的说明。

基本上大多数事情都按预期工作,容器片段被膨胀到FrameLayout 并且他们的标签片段被正确添加到ViewPager 作为标签(需要这样,因为我有很多片段应该重用布局)。

我正在努力解决的问题是FrameLayout(以及因此选项卡片段)占用了整个屏幕高度,因此它与ToolbarTabLayout 重叠。为了可视化问题,我创建了以下图像:

带有CoordinatorLayoutToolbarTabLayout 的基本布局:

<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-class 夸大了所有片段(在另一篇关于 SO 的帖子中,调用 inflater.inflate(getLayoutRes(), null); 是导致相同问题的问题)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(getLayoutRes(), container, false);
}

如果我将CoordinatorLayout 替换为普通的LinearLayout,则FrameLayout 按预期从AppBarLayout 下方开始,但根据documentation AppBarLayout 的大部分功能需要是CoordinatorLayout 的直接子代。

我可以在FrameLayout 中添加一个marginTop,但我想知道是否有任何合适的解决方案。提前感谢您的任何提示!

【问题讨论】:

    标签: android android-layout android-fragments android-support-design


    【解决方案1】:

    将您的 app:layout_behavior="@string/appbar_scrolling_view_behavior" 移动到 FrameLayout - 该属性需要位于 CoordinatorLayout 的直接子级上。

    【讨论】:

    • 这解决了我遇到的同样问题,谢谢,但我不知道为什么。 appbar_scrolling_view_behavior 究竟做了什么……?
    • @MicroR - 我建议阅读AppBarLayout.ScrollingViewBehavior[(http://developer.android.com/reference/android/support/design/widget/AppBarLayout.ScrollingViewBehavior.html) or looking at [the source code 的Javadoc - 它添加了所需的填充以不与AppBarLayout 重叠,并允许AppBarLayout 响应滚动如果穿上可滚动的视图(例如NestedScrollViewRecyclerView
    • @ianhanniballake 我已经使用了你的建议,但问题是我在我的 tablayout 和 framelayout 之间获得了一些额外的空间,我已经应用了 bk 颜色来分析原因,但 UI 中没有出现背景颜色
    • @Erum - 如果你的代码没有单独的问题,就不可能知道你在做什么
    • 对我来说,它有助于将 替换为直接放置工具栏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2016-03-12
    相关资源
    最近更新 更多