着色Android的状态栏在导航抽屉 [英] Coloring Android Status Bar in Nav Drawer

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

问题描述

在这个程序我建设,我添加了一个抽屉式导航片段到我的活动。我使用的是5.0,所以我已经能够设置primaryColor和primaryColorDark得到正确的颜色。我决定尝试和风格我的导航抽屉非常类似谷歌现在的抽屉里5.0,其中导航抽屉有它自己的背景状态栏。 (不能发布图片,没有足够的信誉>>)

In this app I'm building, I've added a Navigation Drawer fragment into my activity. I'm using 5.0, so I've been able to set the primaryColor and primaryColorDark to get the right colors. I've decided to try and style my Nav Drawer to be very similar to Google Now's drawer in 5.0, where the Nav Drawer has it's own background for the status bar. (Can't post pictures, not enough reputation >.>)

我跟着这个问题的建议<一href="http://stackoverflow.com/questions/26440879/how-do-i-use-drawerlayout-to-display-over-the-actionbar-toolbar-and-under-the-st">here,这已经帮了不少忙。我已经取得了画我的导航抽屉下的状态栏,当我拉出来。唯一的麻烦是,我无法弄清楚如何设置状态栏,显示在我的抽屉里的颜色。目前,它只是显示为白色。

I've followed this question's recommendations here, which has helped quite a bit. I've achieved drawing my Nav Drawer under the status bar when I pull it out. Only trouble is, I can't figure out how to set the color of the status bar that shows in my drawer. Currently, it just shows white.

下面是我的风格和code中的相关内容:

Here are the relevant bits of my styles and code:

活动的布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main"
    android:fitsSystemWindows="true">

    <!-- main content -->
    ...

    <!-- Drawer fragment -->
    <fragment
        android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:theme="@style/DrawerTheme"
        android:fitsSystemWindows="true"
        android:choiceMode="singleChoice"
        android:name="com.myname.appname.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

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

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navDrawerFragment"
    android:theme="@style/DrawerTheme"
    android:background="@color/WindowBackground"
    tools:context=".NavigationDrawerFragment"
    android:fitsSystemWindows="true">

     <!-- content here -->

</RelativeLayout>

每以上链接,设置我的状态栏的颜色为活动(这部分工作正常):

Per link above, setting color of my status bar for the activity (this part works correctly):

public void onCreate(Bundled savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ....

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
    drawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

    // ....
}

最后,我的,什么我'尝试'活动相关的方式分配给片段。

And finally, my associated styles for the activity, and what I've 'tried' to assign to the fragment.

<style name="StatusBarActivityTheme" parent="MyAppTheme">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>


<style name="DrawerTheme" parent="MyAppTheme">
    <item name="android:statusBarColor">#000000</item>
    <item name="android:colorPrimaryDark">#000000</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>

在此先感谢您的帮助,您可以发送我的方式!我怀疑我的问题是有关如何处理如何谷歌现在的主屏幕上有一个透明的状态栏,它只有颜色的导航抽屉中,而且任何消息与此相反的将是巨大的!

Thanks in advance for any help you can send my way! I suspect my issue is related to do with how Google Now's primary screen has a transparent status bar, and only colors it during the Nav Drawer, but any news to the contrary would be great!

推荐答案

通过你的设置中,导航抽屉简直就是一个视图绘制背景(这已定义为安卓背景= @色/ WindowBackground)的状态栏下方(其中您通过设置为透明的颜色你的 StatusBarActivityTheme )。该系统不仅看起来尽可能的活动主题,设置状态栏的颜色;分配安卓statusBarColor 来一个孩子有没有影响

With the setup you have, the Nav Drawer is simply a view drawing its background (which you have defined as android:background="@color/WindowBackground") underneath the status bar (the color of which you have set to transparent via your StatusBarActivityTheme). The system only looks as far as the Activity theme to set the status bar color; assigning android:statusBarColor to a child has no effect.

简单的解决办法是为更改您的导航抽屉的片段布局的安卓背景到你想要的颜色。如果你想为下面的图像的部分,以保持白色,一种选择是,然后用 Android的加入你的抽屉布局的空白视图:后台=@色/ WindowBackground或其他颜色。

The simple solution would be to change your Nav Drawer fragment layout's android:background to the color you desire. If you wish for the portion below your image to remain white, an option would be to then add an empty View in your drawer layout with android:background="@color/WindowBackground" or some other color.

如果您希望为的内容的在你的导航抽屉延长下方的状态栏,它需要更多的工作。它之所以被抵消首先是因为你设置的安卓fitsSystemWindows 属性设置为true,这反过来又调用视图的默认<一href="https://developer.android.com/reference/android/view/View.html#fitSystemWindows(android.graphics.Rect)">fitSystemWindows().由于文档说明,这种方法需要的边界(即在我们的情况下,状态栏的高度)并应用它作为视图的填充(在你的情况,这将成为顶部填充为 RelativeLayout的)。

If you desire for the content in your Nav Drawer to extend below the status bar, it requires a bit more work. The reason it is offset to begin with is because you set the android:fitsSystemWindows attribute to true, which in turn calls the view's default fitSystemWindows(). As the docs explain, this method takes the inset (i.e. the height of the status bar in our case) and applies it as the view's padding (in your case, this becomes top padding for the RelativeLayout of your Nav Drawer fragment).

要规避这种填充方法之一是覆盖视图的 fitSystemWindows()方法。我直接给你通过谷歌开源IO调度程序 - 具体而言,他们所使用的<一个href="https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsScrollView.java"><$c$c>ScrimInsetsScrollView作为其导航抽屉的根元素。这种修改滚动型适用于您所选择的颜色的纤维织品(通过自定义的应用程序设置:insetForeground 属性)和消耗的插图,即没有更多的填充!

One way to circumvent this padding is to overwrite the view's fitSystemWindows() method. I direct you to the open source IO Schedule app by Google - specifically, they used the ScrimInsetsScrollView as the root element in their Nav Drawer. This modified ScrollView applies a scrim of a color of your choice (set via the custom app:insetForeground attribute) and consumes the inset, i.e. no more padding!

ScrimInsetsScrollView 可以作为一种模式来编写自己的版本,任何浏览后代,真的 - 看到近了相同的<一个href="https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java"><$c$c>ScrimInsetsFrameLayout.

The ScrimInsetsScrollView can be used as a model to write your own version for any View descendant, really - see the nigh identical ScrimInsetsFrameLayout.

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

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