【发布时间】:2015-08-05 05:59:47
【问题描述】:
我在 MainActivity 中实现了材质导航抽屉和工具栏。为了实现抽屉式导航并确保它显示在状态栏后面,我使用了工具栏.xml 的以下代码
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:fitsSystemWindows="true"
android:theme="@style/ToolbarTheme">
</android.support.v7.widget.Toolbar>
并将其包含在 activity_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"
android:id="@+id/navigation_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
........
在styles.xml中我使用了以下属性,
<item name="android:windowTranslucentStatus">true</item>
这样一切正常,我得到以下结果,
当我单击编辑文本视图 (00.00) 时出现问题。单击页面中的编辑文本视图或任何编辑文本视图的那一刻,工具栏就会展开。见下图:
现在,我可以通过将android:fitsSystemWindows="true" 放入activity_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"
android:id="@+id/navigation_drawer"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
........
我的问题是,虽然这个解决方案有效,但我不确定为什么我必须在布局文件中使用相同的属性两次?是什么让工具栏扩展了?另外,这是解决这个问题的正确方法吗?
编辑 - 从 toolbar.xml 中删除 android:fitsSystemWindows="true" 并将其放置在抽屉布局中会导致主要活动产生正确的结果,但其他活动有一个白条。见下图。
【问题讨论】:
-
您使用了两次哪个属性?而且我相信这是
android:fitsSystemWindows="true"的正确用法,因为它旨在使您的视图适合操作栏或导航栏的边缘 -
一次在toolbar.xml中使用
android:fitsSystemWindows="true"(已经包含在activity_main.xml中),另一次在android.support.v4.widget.DrawerLayout中再次出现在activity_main.xml中。 -
您需要
android:fitsSystemWindows="true"为DrawerLayout以使其适合工具栏的边缘,以防止它进入工具栏下方,但我不确定为什么在工具栏中需要它.xml。如果删除会怎样? -
添加了删除
android:fitsSystemWindows="true"的结果。请查看编辑。 -
stackoverflow.com/questions/28043202/… 这是其中一种解决方案。我试过了,它奏效了。不知道为什么,但这没关系:P
标签: android android-layout toolbar android-toolbar