Android透明顶栏(沉浸式)在android4.4上的实现

来源:互联网 发布:软件结构设计的作用 编辑:程序博客网 时间:2024/04/28 09:28

android的透明顶栏是在android5.0加入的新特性, 虽然其实在ios中早就有了.....:


在5.0中的实现方式很简单:

在style.xml中的application 的主题中添加colorPrimary,即可, 还可以设置更多的默认颜色来进行主题的搭配,当然这不是我今天要说的重点;


为了在android4.4及以上的设备上也有沉浸式的体验,其实我们只要加入下面的代码在Activity的OnCreat中就可以了:


public static void setTransparentStyle(Context context, int colorResourceID){//获取颜色int color = context.getResources().getColor(colorResourceID);//形成完整沉浸式的方法((Activity)context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);SystemBarTintManager tintManager = new SystemBarTintManager((Activity) context);tintManager.setStatusBarTintEnabled(true);  // 设置一个颜色给系统栏   tintManager.setTintColor(color);   // 设置一个状态栏资源 tintManager.setStatusBarTintColor(color);}


这段代码有一个依赖的类, 直接复制到自己的工程中引用即可:

<script src="https://code.csdn.net/snippets/984384.js"></script>



显示效果如图:


0 0
原创粉丝点击