fitsSystemWindows无法在NavigationView

时间:2016-10-09 12:20:33

标签: android layout

我在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>
  

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>

1 个答案:

答案 0 :(得分:7)

因此,如果我理解正确,您希望自定义视图获得必要的填充,以便其状态栏不会剪切其内容吗?

如果是这种情况,则需要设置

android:fitsSystemWindows="true"

在你的根DrawerLayout中,然后设置

android:fitsSystemWindows="false"
您的NavigationView组件中的

。请注意, false ,不是真实的:)

推理:

Google设计的新NavigationView组件使用'fitsSystemWindows'属性来自定义其内容与状态栏的关联方式。请注意,这里的“自定义”是关键字,因为此特定组件的硬编码行为是其内容应与状态栏重叠并到达屏幕顶部,而状态栏本身应该是透明的,以允许抽屉的内容通过它看到。这被指定为新材料设计的一部分,如https://material.io/guidelines/patterns/navigation-drawer.html中所示。

因此,禁用此行为的唯一方法是告诉NavigationView 发信号通知fitsSystemWindow属性,并且只在包含所有其他视图的根DrawerLayout中设置它,这将执行您的操作期待并适当地填补所有孩子的意见。

请注意,Android开发人员Ian Lake的this comment在一篇关于此特定属性的博文中也证实了这种推理。

P.S。

我还会删除navigation_main_drawer XML中所有子元素中'fitsSystemWindows'属性的所有提及,以防万一,虽然它可能确实没有任何影响。

相关问题