【问题标题】:Toggling visibility AppBarLayout views causes spacing issues for loaded Fragments切换可见性 AppBarLayout 视图会导致加载的片段的间距问题
【发布时间】:2015-09-13 16:59:19
【问题描述】:

我在使用新的 Android 设计支持库 (http://android-developers.blogspot.com.ar/2015/05/android-design-support-library.html) 时遇到了一个奇怪的问题。如果我将其他内容(如 LinearLayout)与 ToolBar 一起放置在 AppBarLayout 中并切换该内容的可见性,则切换片段将在片段内容的顶部显示死区。

当切换内容的可见性时,AppBarLayout 似乎没有正确调整父 CoordinatorLayout 的大小。我将 CoordinatorLayout 包裹在 DrawerLayout 中。我想根据显示的片段来切换 AppBarLayout 中额外 LinearLayout 的可见性。

这是我的 MainActivity 的 main.xml 文件:

<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">

<android.support.design.widget.CoordinatorLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent">

        <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"/>

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:gravity="center_vertical"">

                <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="start"
                        android:text="Hello"/>
            </LinearLayout>

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
           app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

    </android.support.design.widget.CoordinatorLayout>

  <android.support.design.widget.NavigationView
            android:id="@+id/navigation"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/drawer"/>

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 这是设计支持库的问题。它被报告并标记为未来版本,抱歉我找不到问题的链接
  • 一定有办法强制重绘。如果旋转视图,间距是固定的。听起来像一个比赛条件。或者也许可以将 AppBarLayout 移动到 Fragment 以避免该问题。我想查看原始错误报告,看看它是否提供了修复。
  • 一个似乎可行的解决方案是将 CoordinatorLayout 中的所有内容添加到 Fragment,并根据每个 Fragment 的需要从 AppBarLayout 添加/删除内容。您需要获取对 ToolBar 的引用才能添加汉堡菜单、添加菜单项或更新标题。
  • 你设置为View.INVISIBLE?试试View.GONE
  • View.Gone 正在被使用,这是一个错误。

标签: android material-design android-coordinatorlayout android-support-design android-appbarlayout


【解决方案1】:

我在使用支持设计小部件时遇到了类似的问题。我在 DrawerLayout 中有一个 CoordinatorLayout,在 CoordinatorLayout 中有一个 AppBarLayout。我在 AppBarLayout 中有两个工具栏。我的目标是显示带有 ViewPager 的工具栏,其中显示 recyclerview 内容。我想在选择项目时在工具栏之间切换。换句话说,我让一个工具栏消失了,而另一个是可见的,反之亦然。向上滚动内容会将工具栏推离屏幕顶部。除了改变方向会为工具栏显示一个应该消失的空间之外,一切都运行良好。我尝试了所有我能想到的方法来摆脱它,但没有成功。然后我遇到了这篇文章,并意识到这是支持库中的一个错误。然后我尝试在 AppBarLayout 中放置一个 FrameLayout,然后将两个工具栏放在 FrameLayout 中,并且没有更多空间!现在一切都按照我的预期工作。 GONE 工具栏已消失,只有可见的工具栏显示,即使在更改方向时也是如此。

希望这对某人有所帮助。

【讨论】:

  • 感谢您分享我的朋友...此解决方案有效!就是这样。
猜你喜欢
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
相关资源
最近更新 更多