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

标签 android android-layout android-navigation

我正在尝试创建一个抽屉导航,类似于 Material 规范中的抽屉导航(比如新 gmail 应用中的抽屉导航)。注意抽屉导航的内容是如何绘制在状态栏后面的:

example from the Material spec

使用来自 this question 的 Chris Banes 的回答,我能够成功地将我的应用程序中的抽屉导航绘制在状态栏后面;这工作正常。不起作用的是在状态栏后面绘制抽屉导航的内容。我希望抽屉中的蓝色图像显示在状态栏的后面,但该区域是用状态栏的颜色绘制的,如此屏幕截图所示。

my app

那么,我怎样才能让我的抽屉导航在状态栏后面的区域中绘制呢?我已经在下面发布了我项目的相关部分。

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

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

我的 Activity 主题

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

在我的 Activity 的 onCreate() 中,我执行以下操作:

mDrawerLayout.setStatusBarBackground(R.color.primary_dark);

最佳答案

适用于 API 21+

<style name="AppTheme" parent="android:Theme.Holo.NoActionBar.TranslucentDecor">
    ...
</style>

适用于 API 19+

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

您的布局应该有 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);

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


ss

关于android - 使抽屉导航在状态栏后面绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26913770/

相关文章:

android - 如何在 Opencv android 中进行匹配

android - 在android中设置listview滚动动画

android - 将 JavaScript 接口(interface)添加到浏览器

java - 尝试添加启动画面时出错 - Android 应用程序

android - Textview 消失但 listview 可见

android - 用户双击时如何运行方法

android - 如何获取 ActionBar MenuItem 的当前位置?

android - 使用导航 Controller 按下后退按钮后如何防止上一个 fragment 出现?

Android Jetpack 导航组件问题,以启动 fragment 作为根目标