Android开发我爱编程

android 沉浸式/透明状态栏

2016-10-31  本文已影响151人  谷歌哥哥

前言

这种效果从android4.4开始引进的,并且在5.0进行了改进。因此,也只能将这一特性应用在android4.4以上的手机,无法做到全部适配。网上已经有很多有关于系统状态栏的解决方案,自己在网上项目中提炼出来的解决方案. 大牛可以忽略本文(*^ω^*)☞先上个效果图

魅族m1 note 4.4.4

实现AppBarLayout.OnOffsetChangedListener 监听

@Override

public voidonOffsetChanged(AppBarLayoutappBarLayout,intoffset) {

if(offset==0) {

if(state!=CollapsingToolbarLayoutState.EXPANDED) {

state=CollapsingToolbarLayoutState.EXPANDED;//修改状态标记为展开SystemUtils.StatusBarDarkMode((Activity)getContext(),type);

}

}else if(Math.abs(offset)>=appBarLayout.getTotalScrollRange()) {

if(state!=CollapsingToolbarLayoutState.COLLAPSED) {

state=CollapsingToolbarLayoutState.COLLAPSED;//修改状态标记为折叠

type=SystemUtils.StatusBarLightMode((Activity)getContext());

}

}else{

if(state!=CollapsingToolbarLayoutState.INTERNEDIATE) {

if(state==CollapsingToolbarLayoutState.COLLAPSED){

state=CollapsingToolbarLayoutState.INTERNEDIATE;//修改状态标记为中间

type=SystemUtils.StatusBarLightMode((Activity)getContext());

}

}

/**

*修改状态栏为全透明

*@paramactivity

*/

@TargetApi(19)

public static voidtransparencyBar(Activityactivity) {

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {

Windowwindow=activity.getWindow();

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

|WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

window.setStatusBarColor(Color.TRANSPARENT);

}else{

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {

Windowwindow=activity.getWindow();

window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

}

}

}

staticSystemBarTintManagertintManager;

/**

*修改状态栏颜色,支持4.4以上版本

*@paramactivity

*@paramcolorId

*/

public static voidsetStatusBarColor(Activityactivity,intcolorId,booleaniskkclose) {

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {

Windowwindow=activity.getWindow();

window.setStatusBarColor(activity.getResources().getColor(colorId));

}else if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {

if(tintManager==null) {

tintManager= newSystemBarTintManager(activity);

}

if(iskkclose) {

tintManager.setStatusBarTintEnabled(true);

}else{

tintManager.setStatusBarTintEnabled(false);

}

tintManager.setStatusBarTintResource(colorId);

}

}

/**

*设置状态栏黑色字体图标,

*适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android

*@paramactivity

*  //白色可以替换成其他浅色系

*/

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public static intStatusBarLightMode(Activityactivity) {

intresult=0;

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {

if(MIUISetStatusBarLightMode(activity.getWindow(),true)) {//MIUI

result=1;

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {//5.0

}else if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {//4.4

activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

}

}else if(FlymeSetStatusBarLightMode(activity.getWindow(),true)) {//Flyme

result=2;

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {//5.0

activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity,R.color.transparent));

}else if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {//4.4

activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

}

}else if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {//6.0

result=3;

activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity,R.color.transparent));

activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

}else{

result=4;

setStatusBarColor(activity,R.color.color_gray_999999,true);

}

}

returnresult;

}

/**

*清除MIUI或flyme或6.0以上版本状态栏黑色字体

*/

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public static voidStatusBarDarkMode(Activityactivity,inttype){

if(type==1){

MIUISetStatusBarLightMode(activity.getWindow(),false);

}else if(type==2){

FlymeSetStatusBarLightMode(activity.getWindow(),false);

}else if(type==3){

activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

Windowwindow=activity.getWindow();

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

window.setStatusBarColor(Color.TRANSPARENT);

}else if(type==4){

setStatusBarColor(activity,R.color.transparent,false);

}

}

/**

*设置状态栏字体图标为深色,需要MIUIV6以上

*

*@paramwindow需要设置的窗口

*@paramdark是否把状态栏字体及图标颜色设置为深色

*@returnboolean成功执行返回true

*/

public static booleanMIUISetStatusBarLightMode(Windowwindow,booleandark) {

booleanresult= false;

if(window!= null) {

Classclazz=window.getClass();

try{

intdarkModeFlag=0;

ClasslayoutParams=Class.forName("android.view.MiuiWindowManager$LayoutParams");

Fieldfield=layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");

darkModeFlag=field.getInt(layoutParams);

MethodextraFlagField=clazz.getMethod("setExtraFlags",int.class,int.class);

if(dark) {

extraFlagField.invoke(window,darkModeFlag,darkModeFlag);//状态栏透明且黑色字体

}else{

extraFlagField.invoke(window,0,darkModeFlag);//清除黑色字体

}

result= true;

}catch(Exceptione) {

}

}

returnresult;

}

/**

*设置状态栏图标为深色和魅族特定的文字风格

*可以用来判断是否为Flyme用户

*

*@paramwindow需要设置的窗口

*@paramdark是否把状态栏字体及图标颜色设置为深色

*@returnboolean成功执行返回true

*/

public static booleanFlymeSetStatusBarLightMode(Windowwindow,booleandark) {

booleanresult= false;

if(window!= null) {

try{

WindowManager.LayoutParamslp=window.getAttributes();

FielddarkFlag=WindowManager.LayoutParams.class

.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");

FieldmeizuFlags=WindowManager.LayoutParams.class

.getDeclaredField("meizuFlags");

darkFlag.setAccessible(true);

meizuFlags.setAccessible(true);

intbit=darkFlag.getInt(null);

intvalue=meizuFlags.getInt(lp);

if(dark) {

value|=bit;

}else{

value&= ~bit;

}

meizuFlags.setInt(lp,value);

window.setAttributes(lp);

result= true;

}catch(Exceptione) {

}

}

returnresult;

}

上一篇下一篇

猜你喜欢

热点阅读