无法将 DrawerLayout 放在 StatusBar 下

2024-03-29

我有一个活动Navigation Drawer并使用ScrimInsetsFrameLayout我能够将布局放在StatusBar一切都很顺利。 然后我决定更换颜色Toolbar and StatusBar with a png所有活动布局的背景。 我在模拟器(Nexus 5 with android 6.0)上运行该应用程序,结果正是我想要的,就像你可以在Image #1下面,但是当我在我的设备(带有 android 5.0 的 Galaxy Note 3)上尝试时,ScrimInsetsFrameLayout 内的布局超出了StatusBar Image #2。 我不明白出了什么问题,你能帮助我吗?

这是我的values-v21 and my activity.xml

<style parent="Theme.AppCompat.Light.NoActionBar" name="AppTheme_Activities">

    <item name="android:colorPrimary">@android:color/transparent</item>
    <item name="android:colorPrimaryDark">@color/insetF</item>
    <item name="android:navigationBarColor">@color/insetF</item>
    <item name="android:colorAccent">@color/primary</item>
    <item name="android:colorEdgeEffect">@color/primary</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:statusBarColor">@color/insetF</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>

</style>



<?xml version="1.0"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawer"
    android:fitsSystemWindows="true"
    android:background="@drawable/background"> <!--png image-->

    <FrameLayout
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <include layout="@layout/toolbar_activities" android:id="@+id/toolbar_layout"/>

        <FrameLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/content_frame">

        </FrameLayout>

    </FrameLayout>

    <com.example.myapplication.ScrimInsetsFrameLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/linearLayout"
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:insetForeground="#4000"
        android:clickable="true"
        android:background="#ffffff"> .....

    </com.example.myapplication.ScrimInsetsFrameLayout>

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

Image #1 enter image description here

Image #2 enter image description here


  1. 添加到您的onCreate() of Activity

        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    
  2. Change ScrimInsetsFrameLayout's android:fitsSystemWindows财产给false

  3. Remove android:fitsSystemWindows="true" from DrawerLayout

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawer"
        android:background="@drawable/background"> <!--png image-->
    
        <FrameLayout
            android:orientation="vertical"
            android:layout_height="match_parent"
            android:layout_width="match_parent">
    
            <include layout="@layout/toolbar_activities" android:id="@+id/toolbar_layout"/>
    
            <FrameLayout
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/content_frame">
    
            </FrameLayout>
    
        </FrameLayout>
    
        <com.example.myapplication.ScrimInsetsFrameLayout
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/linearLayout"
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="false"
            app:insetForeground="#4000"
            android:clickable="true"
            android:background="#ffffff"> .....
    
        </com.example.myapplication.ScrimInsetsFrameLayout>
    
    </android.support.v4.widget.DrawerLayout>
    
  4. 将这些样式添加到您的AppTheme_Activities主题(保持您的状态所需的颜色):

            <item name="windowActionBarOverlay">false</item>
            <item name="android:windowActionBarOverlay">false</item>
            <item name="android:fitsSystemWindows">false</item>
            <item name="android:statusBarColor">#4000</item>
    
  5. 这将导致工具栏也位于状态栏下方,因此您需要执行此操作:

    • Make Toolbar的高度 ="wrap_content"

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

      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
      ......
      ......
      public int getStatusBarHeight() {
          int result = 0;
      
          if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
              int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
              if (resourceId > 0) {
                  result = getResources().getDimensionPixelSize(resourceId);
              }
          }
          return result;
      }  
      

应该就是这样了!

我已将状态栏下方带有导航抽屉的测试应用程序的源代码上传到我的保管箱 -请随意查看。 https://www.dropbox.com/s/elnsloyozyp43u3/TranslucentStatusBarUpdated.zip?dl=0

我不久前回答过非常类似的问题 - 也许你也会发现它很有用:Android 中具有动态 ActionBar 颜色的半透明 StatusBar https://stackoverflow.com/a/34384963/1658267

我希望,它有帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将 DrawerLayout 放在 StatusBar 下 的相关文章

  • Android - NumberPicker 滚动/快速滑动更快

    如何使数字选择器滚动 快速滑动更快 目前从00分钟到59分钟需要付出很大的努力 我尝试过一些例子Android 中 Viewpager 控制器速度减慢 https stackoverflow com questions 8155257 sl
  • 清单合并失败:属性 application@appComponentFactory

    一切都很好 但我正在尝试添加这个库https github com wdullaer MaterialDateTimePicker https github com wdullaer MaterialDateTimePicker有了这个 i
  • 鼠标在两个视图上移动,如何让它们都处理`ACTION_MOVE`事件

    我有两个视图 较大的一个在底部 较小的一个在顶部 如下图所示 现在我在 view1 view2 外部 上按鼠标 然后移动到 view2 我发现即使鼠标在移动过程中位于view2内部 view2也不会得到ACTION MOVE事件 只有vie
  • 如何使用 Retrofit 2 下载 pdf 文件

    我在下载带改造的 pdf 文件时遇到困难 我的代码生成了一个文件 但它的大小错误 并且当 pdf 打开时它是空白的 这就是我的 php web 服务返回 pdf 文件的方式 param Slim Slim app param String
  • 从源代码定制 Android 彩信/短信应用程序?

    好的 这是交易 我想下载 Android 手机附带的彩信 短信应用程序的完整源代码 我希望能够对其进行修改 并向其添加一些自定义功能 我正在使用 Eclipse 进行开发 并且使用的是 Windows 7 我注意到现在有一 个适用于 Win
  • Android XML 解析与 RSS

    我有我的 Rss 文件项目
  • 以编程方式创建进度绘制

    我有一个场景 我需要有大量的进度条可绘制对象 我无法为所有这些创建 xml 资源 因为我希望用户选择一种颜色 然后用于动态创建可绘制对象 下面是 xml 中的一个这样的可绘制对象 我如何以编程方式创建这个精确的可绘制对象
  • 创建 Android 智能应用横幅

    Android 设备有类似 iOS 6 智能应用横幅的解决方案吗 这是智能应用横幅的代码 从 Chrome 44 Beta 开始 您可以在 Android 版 Chrome 中推送您的应用程序 您网站上的本机应用程序安装横幅 请看下面的答案
  • 如何将值从 recyclerview 项目传递到另一个活动

    当我们单击 recyclerview 项目时 我试图将 recyclerview 项目中的值传递给另一个活动 这里我使用的是OnItemTouchListener 我从 JSON 检索数据并将其解析为 ArrayList 我保存了5个参数
  • 任务“:app:checkReleaseDuplicateClasses”执行失败。扑

    请找到以下文件 1 应用程序 构建 gradle def localProperties new Properties def localPropertiesFile rootProject file local properties if
  • 使用 Android 将文件上传到 Django Web 服务

    我正在开发一个与 Django 后端交互的 Android 应用程序 我已经使用 mod wsgi 部署了 Web 服务 并且有许多 Web 服务调用可以工作并且已经过测试 所以我知道一切都应该工作 所有其他呼叫都工作正常 这是我在Andr
  • 将 SearchView 阈值设置为零

    我在用SearchView在android中 我将适配器设置为搜索视图 但建议弹出框在一个字符后显示 因为我将阈值设置为1 它不接受小于1的数字 如何将阈值设置为零 请喜欢 Google 因为您可能知道 Google Play 的应用程序没
  • Android 日期/时间显示 0 而不是 12

    我想知道下面的代码有什么问题 Assign hour set in the picker c set Calendar HOUR selectedHour c set Calendar MINUTE selectedMinute For a
  • 按下后退按钮时停止 Fragments 中的 AsyncTask

    我有一个托管片段的活动 按下按钮会通过 FragmentTransaction 从片段 A 转到片段 B 并将其添加到返回堆栈中 现在片段 B 有一个 AsyncTask 实现 它从 sdcard 加载图像并在加载图像时将其发布 如果我按
  • 在 Android 中下载文件

    我正在使用以下代码在 Android 中下载文件 public class FileDownloadActivity extends Activity ProgressDialog mProgressDialog Called when t
  • Intent.ACTION_DIAL 号码以 # 结尾

    所以我尝试通过以下方式发送号码Intent ACTION DIAL以 结尾 例如 123 但是当Android Dialer应用程序启动时 只有 123 不见了 我正在使用以下代码来触发 Android 的拨号应用程序 Uri number
  • 如何在 Android WebView 中从远程 URL 访问本地资源?

    要加载 HTML 中的资源 我使用 URLfile android asset my image png 当我在本地加载 HTML 时 它可以工作 例如 使用WebView loadData method 但是 我无法从远程网站加载本地资源
  • 如何在真实设备上模拟来电? [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 出于测试目的 我想在真实的 Andr
  • pytube 在 Android 中传输视频所需的时间太长

    我在用pytube在 Android 中流式传输视频 借助chaquopy 视频文件 py from pytube import YouTube def video link yt YouTube f https www youtube c
  • 从多个通知启动活动会覆盖之前的意图

    public static void showNotification Context ctx int value1 String title String message int value2 NotificationManager no

随机推荐