话不多说,我写的上一篇 Android沉浸式状态栏 只针对 系统栏 中的 状态栏,也是最常用的,因为开发项目基本不会出现更改 导航栏 的需求,因为 导航栏 的用语与其他地方有冲突,所以我一般用 虚拟按键栏 代替,也很好理解,就是AndroID手机下方的三个虚拟按键,所以下面所说的更改导航可以了解一下,不排除以后公司产品告诉你需要改颜色。
获取AndroID4.4 - 5.0的系统栏
private static final String STATUS_bar_TAG = "COM_BRAVE_SYstem_bar_liBRARY_STATUS_bar";private static final String NAV_bar_TAG = "COM_BRAVE_SYstem_bar_liBRARY_NAV_bar";private static final VIEw getbar(@NonNull Window window, boolean isstatusbar) { if (isAndroID_5_0_Above()) { return null; } VIEwGroup decorVIEw = (VIEwGroup) window.getDecorVIEw(); String tag = isstatusbar ? STATUS_bar_TAG : NAV_bar_TAG; VIEw bar = decorVIEw.findVIEwWithTag(tag); if (null == bar) { bar = new VIEw(window.getContext()); Context context = window.getContext(); int barHeight = isstatusbar ? Utils.getStatusbarHeight(context) : Utils.getNavbarHeight(context); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, barHeight); params.gravity = Gravity.top; bar.setLayoutParams(params); bar.setTag(tag); decorVIEw.addVIEw(bar); } return bar;}private static final VIEw getStatusbar(@NonNull Window window) { return getbar(window, true);}private static final VIEw getNavbar(@NonNull Window window) { return getbar(window, false);}private static final boolean isAndroID_4_4_Above() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;}private static final boolean isAndroID_5_0_Above() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP;}
设置Flags
private static final voID setFlags(@NonNull Window window, boolean isstatusbar, boolean isNavbar, boolean isBlack, boolean preventShaking) { if (isAndroID_5_0_Above()) { window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYstem_bar_BACKGROUNDS); if (isstatusbar) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } if (isNavbar) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } if (preventShaking) { int option = VIEw.SYstem_UI_FLAG_LAYOUT_FulLSCREEN | VIEw.SYstem_UI_FLAG_LAYOUT_Stable; if (isNavbar) { option = option | VIEw.SYstem_UI_FLAG_LAYOUT_HIDE_NAVIGATION; } if (isBlack) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { option = option | VIEw.SYstem_UI_FLAG_liGHT_STATUS_bar; } } else { option = option | VIEw.SYstem_UI_FLAG_VISIBLE; } window.getDecorVIEw().setsystemUIVisibility(option); } else { if (isBlack) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { window.getDecorVIEw().setsystemUIVisibility(VIEw.SYstem_UI_FLAG_liGHT_STATUS_bar | VIEw.SYstem_UI_FLAG_LAYOUT_FulLSCREEN); } } } } else if (isAndroID_4_4_Above()) { if (isstatusbar) { window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } if (isNavbar) { window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } }}
设置根布局
private static final voID setRootVIEw(@NonNull VIEw rootVIEw, boolean fitSystemwindows, boolean clipTopadding) { if (null != rootVIEw) { if (rootVIEw instanceof VIEwGroup) { VIEwGroup vIEwGroup = (VIEwGroup) rootVIEw; vIEwGroup.setFitsSystemwindows(fitSystemwindows); vIEwGroup.setClipTopadding(clipTopadding); } }}
设置系统栏颜色
public static final voID setSystembarcolor(@NonNull Window window, @NonNull VIEw rootVIEw, boolean isstatusbar, boolean isNavbar, boolean preventShaking, boolean isBlack, boolean fitSystemwindows, boolean clipTopadding, @colorInt int color, @IntRange(from = 0, to = 255) int Alpha) { if (!isstatusbar && !isNavbar) { return; } if (!isAndroID_4_4_Above()) { return; } boolean hasNavigationbar = Utils.hasNavigationbar(window); isNavbar = isNavbar && hasNavigationbar; setFlags(window, isstatusbar, isNavbar, isBlack, preventShaking); int barcolor = calculatecolor(color, Alpha); if (isstatusbar) { if (isAndroID_5_0_Above()) { window.setStatusbarcolor(barcolor); } else if (isAndroID_4_4_Above()) { VIEw statusbar = getStatusbar(window); if (statusbar.getVisibility() != VIEw.VISIBLE) { statusbar.setVisibility(VIEw.VISIBLE); } statusbar.setBackgroundcolor(barcolor); } } if (isNavbar) { if (isAndroID_5_0_Above()) { window.setNavigationbarcolor(barcolor); } else if (isAndroID_4_4_Above()) { VIEw navbar = getNavbar(window); if (navbar.getVisibility() != VIEw.VISIBLE) { navbar.setVisibility(VIEw.VISIBLE); } navbar.setBackgroundcolor(barcolor); } } setRootVIEw(rootVIEw, fitSystemwindows, clipTopadding);}
使用系统栏占位(直接给系统栏上色)
barUtils.setSystembarcolor(getwindow(), ((VIEwGroup) findVIEwByID(androID.R.ID.content)).getChildAt(0), true, true, true, true, true, true, color.BLUE, 255);
设置系统栏不占位(使系统栏透明化)barUtils.setSystembarcolor(getwindow(), ((VIEwGroup) findVIEwByID(androID.R.ID.content)).getChildAt(0), true, true, true, true, false, false, color.BLACK, 0);
完整项目地址 SystemBarUtils
具体使用半透明系统栏
barUtils.setTranslucent(this);
透明系统栏 barUtils.settransparent(this);
设置系统栏颜色 barUtils.setSystembarcolor(this, color.BLUE);
设置头部ImageVIEw barUtils.setSystembarcolor(this, true, false, false, false, false, color.BLACK, 0);
设置抽屉布局 barUtils.setSystembarcolor(this, true, false, false, false, false, color.BLACK, 0);
简单说明一下,抽屉布局与头部ImageVIEw的设置方法相同,但是需要在自己的内容布局进行一步 *** 作,就是需要在内容布局中模拟状态栏,你直接改变你内容布局中的模拟状态栏颜色,即可实现抽屉滑动时颜色。
如果您有其他更多的需求同样可以使用该方法,只需在内容布局中就可以 *** 作一切,当然如果您的整个APP都需要这种状态栏,你可以定义一个BaseActivity,来实现一个基布局。
总结以上是内存溢出为你收集整理的Android中的系统栏全部内容,希望文章能够帮你解决Android中的系统栏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)