【问题标题】:Cannot put DrawerLayout under StatusBar不能将 DrawerLayout 放在 StatusBar 下
【发布时间】:2015-12-24 17:39:21
【问题描述】:

我有一个带有Navigation Drawer 的活动,使用ScrimInsetsFrameLayout 我能够将布局放在StatusBar 下,并且一切正常。 然后我决定将所有活动布局的ToolbarStatusBar 的颜色替换为png 背景。 我在模拟器(带有 android 6.0 的 Nexus 5)上运行该应用程序,结果正是我想要的,就像您在下面的 Image #1 中看到的那样,但是当我在我的设备上尝试时(Galaxy Note 3使用 android 5.0) ScrimInsetsFrameLayout 内的布局高于StatusBar Image #2。 我不明白出了什么问题,你能帮帮我吗?

这是我的values-v21 和我的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>

图片#1

图片#2

【问题讨论】:

  • 如果将ScrimInsetsFrameLayoutandroid:fitsSystemWindows 设置为false 会怎样?
  • DrawerLayout 只是没有在 statusBar 下扩展
  • 查看此链接:stackoverflow.com/questions/26440879/… 它可能会对您有所帮助。
  • 我试过了,但什么都没有......

标签: android navigation-drawer statusbar


【解决方案1】:

  1. 添加到您的onCreate()Activity

        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    
  2. ScrimInsetsFrameLayoutandroid:fitsSystemWindows属性更改为false

  3. DrawerLayout 中删除android:fitsSystemWindows="true"

    <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. 这将表明,工具栏也位于状态栏下方,因此您需要执行此 hack:

    • 使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" /> 
      
    • 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;
      }  
      

应该是这样的!

我已将状态栏下方带有导航抽屉的测试应用程序的源代码上传到我的保管箱 - feel free to check it out.

不久前我已经回答了非常相似的问题 - 也许你也会发现它很有用:Translucent StatusBar with dynamic ActionBar color in Android

希望对你有帮助

【讨论】:

  • 我做到了,但现在我有一个透明的状态栏
  • @Pier 应该是什么颜色?
  • 我设置了@color/insetF,也就是app:insetForeground="的透明色#4000 "
  • @Pier 好消息。即使系统影子不适合您,也有适合您的解决方法。我添加了 步骤 1 并修改了**步骤 4**(将 windowTranslucentStatus 替换为 statusBarColor 属性)。看看吧!
  • 没什么,状态栏还是透明的
【解决方案2】:

我遇到了同样的问题,皮尔。烦人的事情是似乎没有任何像样的指南来说明如何解决这个问题,即使它是材料设计指南等的一部分。

不管怎样,我就是这样做的……


样式

我的styles.xml 包含一个基本主题,然后是我的主要应用程序主题。我为使用导航抽屉的活动创建了另一个主题,如下所示:

<style name="Base.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

<style name="AppTheme" parent="Base.AppTheme">
    <item name="colorPrimary">@color/theme_primary</item>
    <item name="colorPrimaryDark">@color/theme_primary_dark</item>
    <item name="colorAccent">@color/theme_accent</item>
</style>

<style name="AppTheme.NavDrawer">
    <item name="android:windowBackground">@color/window_background</item>
</style>

颜色window_background 引用了我的colors.xml 文件,该文件将其列为#FFF5F5F5。请注意,以上对 API 21+ 的工作方式略有不同,因此在 values-v21 目录中,创建另一个 styles.xml。这应该包括以下内容:

<style name="AppTheme.NavDrawer">
    <item name="android:windowBackground">@color/window_background</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

布局

现在是布局。在布局文件中,您将拥有您的导航抽屉,您可以包含以下内容。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <!-- The main content view -->
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/main_adView">

            <!-- The app bar -->
            <android.support.design.widget.AppBarLayout
                android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.v7.widget.Toolbar
                    android:id="@+id/main_toolbar"
                    style="@style/AppTheme.Toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_scrollFlags="scroll|enterAlways"/>
            </android.support.design.widget.AppBarLayout>

            <!-- Your layout content below -->
            <FrameLayout
                android:id="@+id/your_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.design.widget.CoordinatorLayout>

        <!-- Other stuff here if you like -->

    </RelativeLayout>

    <!-- The navigation drawer -->
    <include layout="@layout/navdrawer"
        android:id="@+id/main_navigationView" />

如果您不需要其中的任何其他视图,显然您可以删除RelativeLayout,因为它是多余的。我在&lt;include&gt; 标签中引用的导航抽屉如下:

<android.support.design.widget.NavigationView
    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="@dimen/navdrawer_width"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/navdrawer_header"
    app:menu="@menu/navdrawer_items" />

使用上面的代码,您应该可以在状态栏后面正确绘制导航抽屉。

【讨论】:

  • 不幸的是我有同样的问题。我想知道这是样式问题还是布局问题...当我为 android:colorPrimaryandroid:colorPrimaryDark 使用颜色时,一切都很棒
  • @Pier 我认为如果您的最低 API 级别设置为低于 21,那么您应该使用 colorPrimary(与旧设备兼容),但如果您只是为 API 21+ 制作应用程序,那么你可以使用android:colorPrimary。老实说,我认为总体上这个问题是通过改变样式和布局来解决的。
  • 我在 values 文件夹和 android:colorPrimaryDark 中有 styles.xmlcolorPrimary对于 values-v21 中的 styles.xml
  • 应该没问题 - 我不认为颜色值有区别。尤其是基本主题和导航主题,以及布局文件中使用的 Android 标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2023-04-05
  • 2021-10-18
  • 1970-01-01
相关资源
最近更新 更多