[译] 5 分钟让 Drawer 在状态栏下可见

你也许听过谷歌最新的设计理念 Material Design (“质感设计”)规范,能够让你的抽屉式导航栏跨越整个屏幕,包括状态栏,而且让抽屉后的全部控件以灰暗的网格形式可见。html

然而,许多应用打开抽屉式导航栏时看来是这样的android

这里将示范如何把这些元素改形成上面说到的规范。git

扩展你的主题

你可能已经给包含抽屉的 Activity 定义了一个样式。github

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>  
复制代码

第一步,建立一个扩展自AppTheme的新Themeapp

vaules/styles.xml布局

<style name="AppTheme.NoActionBar">  
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
复制代码

v21/styles.xmlthis

<style name="AppTheme.NoActionBar">  
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>  
复制代码

而且确保你的 Activity 指定使用了这个 theme,好比google

<activity  
    android:name="MyDrawerNavActivity"
    android:theme="@style/AppTheme.NoActionBar"
复制代码

配置 DrawerLayout 控件

第二步,到你定义DrawerLayout控件的地方,设置insetForegroundColor (若是你不想控制 ScrimInsetLayout的颜色,你也能够不设置)。并设置好 fitsSystemWindow 属性值spa

<android.support.v4.widget.DrawerLayout  
    ...
    android:fitsSystemWindows="true"
    app:insetForeground="@color/inset_color"
    >
复制代码

看起来这样翻译

固然,若是一会你想在代码里改变状态栏的颜色或ScrimInsetLayout的颜色,你能够在DrawerLayout中经过setters方法来获取并改变。

drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(this, R.color.wierd_green));  

drawerLayout.setScrimColor(ContextCompat.getColor(this, R.color.wierd_transparent_orange));  
复制代码

感谢你的阅读,若是在我分享的内容里,你有更好的方法来实现,那么在评论里更正,感激涕零。

* 如下添加于 6月5, 2016*

若是你继承 DrawerLayout

Android Support 兼容包(AppCompat) 会在 DrawerLayout 里加入一个 android.support.design.internal.ScrimInsetsFrameLayout, 但若是你使用继承自 DrawerLayout 的自定义控件则不会这么作。

若是你继承了DrawerLayout 可是没有加入ScrimInsetsFrameLayout,你须要这么作:

activity_with_drawer_layout.xml

<com.myproject.views.MyDrawerLayout  
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

    <fragment
        android:id="@+id/left_drawer"
        android:name="com.myproject.fragments.NavigationFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
         />

</com.myproject.views.MyDrawerLayout>  
复制代码

在你的抽屉布局文件中加入一个 ScrimInsetsFrameLayout,如:

navigation_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>  
<android.support.design.internal.ScrimInsetsFrameLayout  
    ...
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    >

    <!--- content of drawer here --->

</android.support.design.internal.ScrimInsetsFrameLayout>复制代码
相关文章
相关标签/搜索