抽屉式导航栏半透明在状态栏不工作 [英] Navigation Drawer semi-transparent over status bar not working

查看:547
本文介绍了抽屉式导航栏半透明在状态栏不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在Android项目,我实现了导航抽屉。我通过新的材料设计规格和<一href="http://android-developers.blogspot.co.uk/2014/10/material-design-on-android-checklist.html">Material设计清单。
该规范说,滑出窗格应该浮高于一切,包括状态栏是半透明的在状态栏中。

I am working on Android project and I am implementing the Navigation Drawer. I am reading through the new Material Design Spec and the Material Design Checklist.
The spec says that the slide out pane should float above everything else including the status bar and be semi-transparent over the status bar.

我的导航面板在状态栏,但它没有得到任何的透明度。我跟着这个 SO 发表了code作为谷歌开发者博客点建议,上述<链接href="http://stackoverflow.com/questions/26440879/how-do-i-use-drawerlayout-to-display-over-the-actionbar-toolbar-and-under-the-st/26440880">How我用DrawerLayout显示在动作条/工具栏,并在状态栏?。

My navigation panel is over the status bar but its not got any transparency. I've followed the code from this SO post as suggested in the Google developers blog spot, link above How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?.

下面是我的XML布局

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/appPrimaryColour" />
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout"
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:fitsSystemWindows="true"
        android:background="#ffffff">
        <ListView android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"></ListView>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

下面是我的应用程序的主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/appPrimaryColour</item>
        <item name="colorPrimaryDark">@color/appPrimaryColourDark</item>
        <item name="colorAccent">@color/appPrimaryColour</item>
        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>

    </style>

下面是我的应用程序V21主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/appPrimaryColour</item>
    <item name="colorPrimaryDark">@color/appPrimaryColourDark</item>
    <item name="colorAccent">@color/appPrimaryColour</item>
    <item name="windowActionBar">false</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

下面是我的onCreate方法

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);

    mDrawerLayout = (DrawerLayout)findViewById(R.id.my_drawer_layout);
    mDrawerList = (ListView)findViewById(R.id.left_drawer);

    mDrawerLayout.setStatusBarBackgroundColor(
        getResources().getColor(R.color.appPrimaryColourDark));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        LinearLayout linearLayout = 
            (LinearLayout)findViewById(R.id.linearLayout);
        linearLayout.setElevation(30);
    }

下面是一个截图我的抽屉式导航显示上的不半透明

Below is a screenshot of my navigation drawer showing the top isn't semi transparent

推荐答案

您的状态栏的背景是白色的,你的抽屉的LinearLayout的背景。为什么?你是设置fitsSystemWindows =真为您DrawerLayout和里面的的LinearLayout。这将导致您的LinearLayout拓展状态栏的后面(这是透明的)。因此,使背景状态栏白色的抽屉组成部分。

Your status bar background is white, the background of your drawer LinearLayout. Why? You are settings fitsSystemWindows="true" for your DrawerLayout and the LinearLayout inside it. This causes your LinearLayout to expand behind the status bar (which is transparent). Thus, making the background for the drawer part of the status bar white.


如果你不想让你的抽屉状态栏的后面延伸(希望有一个半透明的背景为全状态栏),你可以做两件事情:


If you don't want your drawer to extend behind the status bar (want to have a semi-transparent background for the whole status bar), you can do two things:

1)你可以简单地去掉你的LinearLayout任何背景值和颜色里面的内容的背景。或

1) You can simply remove any background value from your LinearLayout and color the background of your content inside it. Or

2)你可以从你的LinearLayout删除 fitsSystemWindows =真正的。我认为这是一个更合乎逻辑,更清洁的方式。您还避免了一层阴影正在施放下的状态栏,在您的抽屉式导航栏不会延伸。

2) You can remove fitsSystemWindows="true" from your LinearLayout. I think this is a more logical and cleaner approach. You will also avoid having a shadow being cast under the status bar, where your navigation drawer doesn't extend.


如果你希望你的抽屉状态栏后面延伸并具有半透明,状态栏大小的叠加,你可以使用<一个href="https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java">ScrimInsetFrameLayout作为一个容器为你的抽屉的内容(ListView控件),并设置状态栏的背景使用应用程序:insetForeground =#4000。当然,你可以改变#4000 来你想要的任何东西。不要忘记保持 fitsSystemWindows =真正的在这里!


If you want your drawer to extend behind the status bar and have a semi-transparent, status bar sized overlay, you can use a ScrimInsetFrameLayout as a container for your drawer content (ListView) and set the status bar background using app:insetForeground="#4000". Of course, you can change #4000 to anything you want. Don't forget to keep fitsSystemWindows="true" here!

或者,如果你不想覆盖您的内容,只显示纯色,你可以设置你的LinearLayout到任何你想要的背景。不要忘了,虽然分别设置你的内容的背景!

Or if you don't want to overlay your content and only display a solid color, you can just set the background of your LinearLayout to anything you want. Don't forget to set the background of your content separately though!

这篇关于抽屉式导航栏半透明在状态栏不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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