Android Lollipop Statusbar Semi-transparent

技术分享

将Navigation Drawer显示在Toolbar的前面,Statusbar的后面,并且Statusbar为semi-transparent的样子。

在项目中加入ScrimInsetsFrameLayout.java:

https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java

并在Drawer Layout的RecyclerView外加上ScrimInsetsFrameLayout:

 

activity_main.xml

    <!-- The navigation drawer -->

    <com.whisper.myapplication.ScrimInsetsFrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:elevation="8dp"
        app:insetForeground="#4000" >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:background="#FAFAFA"
            android:scrollbars="vertical" />

    </com.whisper.myapplication.ScrimInsetsFrameLayout>

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

 

themes.xml(v21)

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.AppTheme" parent="@style/AppBaseTheme">

        <!-- 顶部状态栏颜色 -->

        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>

        <!-- 底部导航栏颜色 -->
        <item name="android:navigationBarColor">#0288D1</item>

    </style>

</resources>

 

增加一个为ScrimInsetsFrameLayout服务的attrs.xml

<resources>
    <declare-styleable name="ScrimInsetsView">
        <attr format="reference|color" name="insetForeground">
        </attr></declare-styleable>
</resources>

 

参考文章:

NAVIGATION DRAWER WITH TRANSPARENT STATUS BAR
 
Navigation Drawer semi-transparent over status bar not working
 
 
注:
一开始我将xml文件写成如下的样式:
    <!-- The navigation drawer -->

    <com.whisper.myapplication.ScrimInsetsFrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:elevation="8dp"
        app:insetForeground="#4000" >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:background="#FAFAFA"
            android:scrollbars="vertical" >
        </android.support.v7.widget.RecyclerView>
    </com.whisper.myapplication.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
项目不认app:insetFroeground="#4000"语句。也许是xml格式组织错误?
不知道为什么,一开始复制ScrimInsetsFrameLayout.java到项目中时,Statusbar一直只显示透明,app:insetForeground="#4000"并不起作用。后来我将整个ScrimInsetsFrameLayout.java的内容重新复制粘贴了一遍,忽然就对了。。。

 

 
 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。