Android DrawerLayout使用StatusBarUtil设置状态栏颜色失败的解决办法
来源:互联网 发布:java sftp上传文件 编辑:程序博客网 时间:2024/06/05 02:09
本文是处于5.0以上系统的环境下进行的,以下暂时未能验证。
众所周知,4.4以下状态栏一般是无法改变颜色的,就是系统默认的黑色。4.4以上之后,系统可以设置状态栏颜色了。一般我们在进行状态栏适配的时候,就会分三种情况,4.4以下、4.4以上、和5.0以上。
之前我在适配的时候,4.4以上选择的半透明,5.0以上是不透明,也就是ToolBar是什么颜色,状态栏就是什么颜色。勉强称之为沉浸式状态栏。
问题就出现在了这里,当DrawerLayout内的状态栏怎么设置颜色?
- 所以这里我使用了第三方–StatusBarUtil,它有一个方法,setColorNoTranslucentForDrawerLayout可以设置不透明的状态栏颜色。此时我的布局时这个样子的。如下:
<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"> <android.support.design.widget.CoordinatorLayout android:id="@+id/main_coord" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" > <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/main_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout> <com.bzh.dytt.base.widget.XViewPager android:id="@+id/main_viewPager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/></android.support.v4.widget.DrawerLayout>
- 设置结果是失败,状态栏是黑色,依然不能改变状态栏的颜色。于是,我翻了翻StatusBarUtil的源码,看看它是如何给DrawerLayout的状态栏改变颜色的。源码如下:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } // 生成一个状态栏大小的矩形 View statusBarView = createStatusBarView(activity, color); // 添加 statusBarView 到布局中 ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); contentLayout.addView(statusBarView, 0); // 内容布局不是 LinearLayout 时,设置padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); } // 设置属性 ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1); drawerLayout.setFitsSystemWindows(false); contentLayout.setFitsSystemWindows(false); contentLayout.setClipToPadding(true); drawer.setFitsSystemWindows(false); addTranslucentView(activity, statusBarAlpha);
它的原理是这样的,先判断版本,然后将状态栏设置为透明,然后给包含ToolBar的这个主布局,顶端再加一个矩形块,这个矩形块的颜色和ToolBar的颜色一致。
在我这里的问题是,DrawerLayout内左侧菜单栏就是NavigationView,而主布局就是CoordinatorLayout。所以这里就是要给CoordinatorLayout的顶端再加一个矩形块替代状态栏。
而源码里又有这样一段代码,if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null)
- 所以最终的解决办法是:给CoordinatorLayout外层再裹一层LinearLayout。
- 此时布局如下:
<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.CoordinatorLayout android:id="@+id/main_coord" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" > <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/main_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout> <com.bzh.dytt.base.widget.XViewPager android:id="@+id/main_viewPager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout></LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/></android.support.v4.widget.DrawerLayout>
以上
2 0
- Android DrawerLayout使用StatusBarUtil设置状态栏颜色失败的解决办法
- android设置状态栏的颜色
- android 设置状态栏的颜色
- Android 使用SystemBarTint设置状态栏颜色
- Android 使用SystemBarTint设置状态栏颜色
- Android设置状态栏颜色
- android设置状态栏颜色
- Android 设置状态栏颜色
- Android 状态栏颜色设置
- android 设置手机状态栏的背景颜色
- Android设置顶部状态栏的颜色
- Android--------工具类StatusBarUtil实现完美状态栏
- android 设置状态栏的颜色跟项目颜色一致
- android 系统状态栏的 换颜色-- 自己设置颜色
- 使用StatusBarUtil工具实现沉浸式状态栏/变色状态栏
- Android状态栏颜色设置(沉浸式状态栏)
- Android 状态栏透明、自定义状态栏颜色设置
- Android 设置状态栏颜色&&沉浸式状态栏
- springMVC工作原理
- golang: 官博译文:可测试的 Golang 代码示例
- root.sh Fails to Start HAIP as Default Gateway is Configured for Private Network VLAN (ID 1366211)
- JavaScript里的match和正则表达式
- socket之面向连接编程(TCP)
- Android DrawerLayout使用StatusBarUtil设置状态栏颜色失败的解决办法
- iframe框架自适应高度
- 通过阅读源码分析elasticsearch中分片如何分配到集群中节点
- 简单分析RTMP规范
- 儿童节,却有超过6000万的儿童无人陪伴
- Python知识点系统图
- 2 cat 命令
- 在javaweb中的jquery ajax中的traditional
- android 固定图片长度和宽度的方法