使导航抽屉在状态栏后面绘制

Nat*_*ers 43 android android-layout android-navigation

我正在尝试创建一个Nav Drawer,就像Material spec中的那个(就像新gmail应用程序中的那个).请注意导航抽屉的内容如何在状态栏后面绘制:

材料规格中的示例

使用Chris Banes从这个问题的答案,我能够成功地使我的应用程序中的导航抽屉在状态栏后面绘制; 这工作正常.什么是无效的是绘制状态栏后面的导航抽屉的内容.我希望抽屉中的蓝色图像显示在状态栏后面,但该区域使用状态栏的颜色绘制,如此屏幕截图所示.

我的应用

那么,如何在状态栏后面的区域中绘制导航抽屉?我在下面发布了我项目的相关部分.

包含导航抽屉的基本布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/warning_container" />

    <FrameLayout
        android:id="@+id/navigation_drawer_fragment_container"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start">

        <fragment
            android:id="@+id/navigation_drawer_fragment"
            android:name="com.thebluealliance.androidclient.fragments.NavigationDrawerFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_navigation_drawer" />

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

我活动的主题

<style name="AppThemeNoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

onCreate()我的活动中,我做了以下事情:

mDrawerLayout.setStatusBarBackground(R.color.primary_dark);
Run Code Online (Sandbox Code Playgroud)

Sim*_*mas 52

适用于API 21+

<style name="AppTheme" parent="android:Theme.Holo.NoActionBar.TranslucentDecor">
    ...
</style>
Run Code Online (Sandbox Code Playgroud)

适用于API 19+

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

你的布局应该有android:fitsSystemWindows="false"(这是默认的).


既然你想要切换半透明度,你可以以编程方式进行:

Window window = getWindow();

// Enable status bar translucency (requires API 19)
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// Disable status bar translucency (requires API 19)
window.getAttributes().flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// Set a color (requires API 21)
window.setStatusBarColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)

我把所有sdk版本检查留给你:)


SS


jpa*_*ogo 17

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true">

        <include layout="@layout/toolbar" />
        <!-- Main layout -->
        <FrameLayout
            android:id="@+id/main_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <!-- Nav drawer -->
    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.example.DrawerFragment"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:fitsSystemWindows="true" />

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

值/的themes.xml

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@color/primary</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
</style>

<style name="AppTheme" parent="AppTheme.Base">
</style>
Run Code Online (Sandbox Code Playgroud)

值-V19 /的themes.xml

<style name="AppTheme" parent="AppTheme.Base">
    <!--This makes the status bar transparent in KK and Lollipop-->
    <!--You do not need values-v21 and if you create them make sure you extend from this one-->
    <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

如果你想改变状态栏的颜色(不同于透明黑色),你将需要使用自定义视图去寻找另一种方法,因为只有当这个DrawerLayout适合SystemWindows()时才会被激活,如果你这样做了不会在状态栏后面但在它下面. mDrawerLayout.setStatusBarBackgroundColor(int)android:fitsSystemWindows="true"


Nat*_*ers 11

我找到了在Android 5.0上执行此操作的最佳方法.关键是使用a ScrimInsetFrameLayout作为导航抽屉的根元素(第二个视图中DrawerLayout).这将使内容扩展以填充状态栏后面的空间.要正确着色插图,可以在以下位置设置以下属性ScrimInsetFrameLayout:

app:insetForeground="#4000"
Run Code Online (Sandbox Code Playgroud)

另外,请确保您有android:fitsSystemWindows="true"稀松布布局!

ScrimInsetFrameLayout可以在此处找到该源代码:https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout. java的

  • 不需要ScrimInsetFrameLayout.我不知道为什么这个答案被接受了. (3认同)