【发布时间】: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