即使使用windowTranslucentStatus和fitsSystemWindows,CoordinatorLayout也不会在状态栏后面绘制 [英] CoordinatorLayout not drawing behind status bar even with windowTranslucentStatus and fitsSystemWindows

本文介绍了即使使用windowTranslucentStatus和fitsSystemWindows,CoordinatorLayout也不会在状态栏后面绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图像这样在状态栏后面绘制视图:

I am trying to draw views behind the status bar like this:

我尝试使用推荐的技术来产生这种效果,但是我明白了:

I tried to produce this effect with the recommended techniques, but I get this:

从屏幕截图中可以清楚地看出,我的应用程序内容均未在状态栏后面绘制.

It's clear from the screenshot that none of my app content is being drawn behind the status bar.

有趣的是,导航抽屉以某种方式设法在状态栏后面绘制:

What's interesting is that somehow, the Nav Drawer manages to draw behind the status bar:

我所做的事情:

  • 使用支持库窗口小部件-CoordinatorLayoutAppBarLayoutToolbarDrawerLayout
  • 在我的应用主题中
  • windowTranslucentStatus设置为true
  • 在我的CoordinatorLayout 上将
  • fitsSystemWindows设置为true
  • Use support library widgets - CoordinatorLayout, AppBarLayout, Toolbar, DrawerLayout
  • windowTranslucentStatus set to true in my app theme
  • fitsSystemWindows set to true on my CoordinatorLayout

这是我的应用主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@android:color/transparent</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:windowTranslucentStatus">true</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:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout android:id="@+id/page_container"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fitsSystemWindows="true"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

我的活动布局中的FrameLayout替换为以下片段布局:

The FrameLayout in my activity layout is replaced with this fragment layout:

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

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:paddingLeft="@dimen/activity_horizontal_margin"
                 android:paddingRight="@dimen/activity_horizontal_margin"
                 android:paddingTop="@dimen/activity_vertical_margin"
                 android:paddingBottom="@dimen/activity_vertical_margin"
                 android:background="@android:color/holo_blue_bright"
                 tools:context=".MainActivity">

        <TextView android:text="@string/lorem_ipsum"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
    </FrameLayout>

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:elevation="0dp"
        android:theme="@style/AppTheme.TransparentAppBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:title="@string/hello_blank_fragment"
            app:popupTheme="@style/AppTheme.OverflowMenu" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

推荐答案

供以后的读者关于该主题以及评论中的问题,还有很多很好的信息,请务必阅读通过他们.

edit for future readers: there's a lot of good information on the subject and the issue on the comments too, make sure to read through them.

原始答案: 您的主题是错误的,这就是原因. 不幸的是,在Kitkat或Lollipop中激活的方式有所不同.在我的代码中,我是用Java实现的,但是如果要使用资源树上的V21文件夹,也可以用XML来实现.参数的名称将与Java对应的名称非常相似.

original answer: Your theme is wrong, that's why. Unfortunately, there're differences on how to activate in in Kitkat or Lollipop. On my code I did it in Java, but you can also achieve it in XML if you want to play with the V21 folders on your resources tree. The name of the parameters will be very similar to the Java counterparts.

从XML中删除android:windowTranslucentStatus并在Java中这样使用:

Delete the android:windowTranslucentStatus from your XML and in Java use like that:

   public static void setTranslucentStatusBar(Window window) {
      if (window == null) return;
      int sdkInt = Build.VERSION.SDK_INT;
      if (sdkInt >= Build.VERSION_CODES.LOLLIPOP) {
         setTranslucentStatusBarLollipop(window);
      } else if (sdkInt >= Build.VERSION_CODES.KITKAT) {
         setTranslucentStatusBarKiKat(window);
      }
   }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
   private static void setTranslucentStatusBarLollipop(Window window) {
      window.setStatusBarColor(
             window.getContext()
                   .getResources()
                   .getColor(R.color. / add here your translucent color code /));
   }

   @TargetApi(Build.VERSION_CODES.KITKAT)
   private static void setTranslucentStatusBarKiKat(Window window) {
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
   }

然后您可以通过活动setTranslucentStatusBar(getWindow());

使状态栏变得半透明并在其后面绘制(由于某种原因,我无法理解)是Android中的两个独立任务.

making the status bar translucent and drawing behind it (for some reason I cannot understand) are two separate tasks in Android.

我已经在代码上进行了更多研究,并且可以肯定的是,我的布局中的android:fitsSystemWindows="true"CoordinatorLayout多了很多.

I've looked more on my code and I'm for sure have A LOT more android:fitsSystemWindows="true" on my layout than just the CoordinatorLayout.

下面是我的布局上所有带有android:fitsSystemWindows="true"的视图:

below are all the Views on my layout with android:fitsSystemWindows="true" on them:

  • CoordinatorLayout
  • AppBarLayout
  • CollapsingToolbarLayout
  • ImageView(带有背景图片)
  • FrameLayout(带有标题的内容)

所以我的建议是测试/尝试在XML上填充android:fitsSystemWindows="true".

so my suggestion is to just test/try filling up android:fitsSystemWindows="true" on your XML.

这篇关于即使使用windowTranslucentStatus和fitsSystemWindows,CoordinatorLayout也不会在状态栏后面绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆