makisang / android

0 stars 0 forks source link

NavigationDrawer 显示在StatusBar之上 #2

Open makisang opened 8 years ago

makisang commented 8 years ago

布局如下,主要是 DrawerLayoutNavigationView 中都要加入 android:fitsSystemWindows="true"

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/my_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

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

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"/>

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

另外,在 Activity 的主题中加上这两条(需要API>21):

        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>

但是,加上这两条item之后你会发现StatusBar变灰,因此需要再加上两条以实现最终效果:

        <item name="colorPrimary">@color/light_blue_colorPrimary</item>
        <item name="colorPrimaryDark">@color/light_blue_colorPrimaryDark</item>
makisang commented 8 years ago

在 activity 中定制 NavigationView 的内容需要用到 inflateHeaderView(int res)inflateMenu(int resId)两个API