使用fitsSystemWindows时,NavigationView没有灰色边框

Sae*_*nic 1 android navigation-drawer material-design

我用Android Studio 2.0创建了一个新的应用程序,我希望将我的导航抽屉放在下面Toolbar.我知道Material Design规格说它应该在它之上,Toolbar但我的抽屉里没有那么多条目,我想看看谷歌提供的整洁的图标动画.

我把它DrawerLayout放在顶层,以获得我想要的东西CoordinatorLayout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.company.app.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:openDrawer="start">

        <include layout="@layout/content_main" />

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

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

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

android:fitsSystemWindow="true"在顶级布局上使用,所以我可以着色状态栏.但是当我这样做时,导航抽屉确实有一个灰色的顶部边框.

在此输入图像描述

当我按照Google指定的方式(在工具栏上方)使用它时,边框会很好但这样看起来很丑陋.有什么办法可以删除吗?

我尝试重写NavigationView.onInsetsChanged(Rect rect),并能够将项目放在顶部,但灰色边框仍然存在.

Bri*_*ian 10

我也很头疼,试图弄清楚这一点并设法找到这个描述如何CoordinatorLayout传递fitsSystemWindows给它的孩子.我肯定认为这是一个错误,因为NavigationView它已经设置false,但后来我读到这个:

NavigationView为您处理状态栏的稀松布保护,确保您的NavigationView在API21 +设备上与状态栏进行适当的交互.

来自Android开发者博客.所以我不确定该怎么想.我认为这可能是由于NavigationView从未打算以这种方式使用的事实.

尽管如此,这是一个你可以尝试解决灰色条问题的解决方案,但我还没弄清楚它是否会导致问题.如果是这样,请回复.

activity_main.xml:

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:fitsSystemWindows="false"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity课堂上:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            navigationView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                    return insets;
                }
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应该得到你这个:

截图