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


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

在style. 中的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);	}


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

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



显示效果如图:


收藏 打印