我在NavigationView中有一个自定义视图.问题是无论在什么组合中,fitsSystemWindows都不在NavigationView中工作.并且抽屉中的顶部项目始终位于transcludent状态栏后面.
main_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorPrimaryBottomBar"
android:fitsSystemWindows="true">
<include layout="@layout/navigation_main_drawer" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
navigation_main_drawer
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:paddingBottom="@dimen/default_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout
...
</LinearLayout>
<View
... />
<LinearLayout
...
</LinearLayout>
<View
... />
<LinearLayout
...
</LinearLayout>
<View
... />
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
所以,如果我理解正确,你希望你的自定义视图获得必要的填充,以便其状态栏不会剪切其内容吗?
如果是这种情况那么你需要设置
android:fitsSystemWindows="true"
Run Code Online (Sandbox Code Playgroud)
在你的根DrawerLayout中,然后设置
android:fitsSystemWindows="false"
Run Code Online (Sandbox Code Playgroud)
在您的NavigationView组件中.注意这是假的,不是真的:)
推理:
Google设计的新NavigationView组件使用'fitsSystemWindows'属性来自定义其内容与状态栏的关联方式.请注意,这里的"自定义"是关键字,因为此特定组件的硬编码行为是其内容应与状态栏重叠并到达屏幕顶部,而状态栏本身应该是透明的,以允许抽屉的内容为通过它看到.这是新材料设计的一部分,可以在https://material.io/guidelines/patterns/navigation-drawer.html中看到.
因此,禁用此行为的唯一方法是告诉NavigationView 不要发出fitsSystemWindow属性的信号,并且只在包含所有其他视图的根DrawerLayout中设置它,这将执行您期望的操作并适当地填充其所有子视图.
请注意,Android开发人员Ian Lake在一篇关于此特定属性的博客文章中发表的评论也证实了这一推理.
PS
我还会删除你的navigation_main_drawer XML中所有子元素中'fitsSystemWindows'属性的所有提及,以防万一,尽管它可能没有任何影响.