Android状态栏以及导航栏设置

tech2023-12-26  72

使用依赖: implementation ‘com.gyf.barlibrary:barlibrary:2.2.8’

public static void initBar(Context context){ // 沉浸状态栏 ImmersionBar.with((Activity) context) .fitsSystemWindows(true) .statusBarColor(R.color.colorPrimary) .init(); } //不显示状态栏 public static void hideBar( Context context){ ImmersionBar.with((Activity) context) .fullScreen(true) .hideBar(BarHide.FLAG_HIDE_BAR) .init(); } //滑动隐藏 Activity public static void slideActivity(Context context){ SlidrConfig config = new SlidrConfig.Builder() .position(SlidrPosition.LEFT) .edge(true) .build(); Slidr.attach((Activity) context, config); } public static void destoryImmersionBar(){ if (mImmersionBar != null){ mImmersionBar.destroy(); //不调用该方法,如果界面bar发生改变,在不关闭app的情况下,退出此界面再进入将记忆最后一次bar改变的状态 } }

.hideBar()参数:

public enum BarHide { FLAG_HIDE_STATUS_BAR, //隐藏状态栏 FLAG_HIDE_NAVIGATION_BAR, //隐藏导航栏 FLAG_HIDE_BAR, //隐藏状态栏和导航栏 FLAG_SHOW_BAR //显示状态栏和导航栏 }

计算导航栏高度方法:

/** * 获取导航栏高度 */ public static int getNavigationBarHeight(Activity context) { int height; Display display = context.getWindow().getWindowManager().getDefaultDisplay(); Point point = new Point(); display.getRealSize(point); View decorView = context.getWindow().getDecorView(); Configuration conf = context.getResources().getConfiguration(); if (Configuration.ORIENTATION_LANDSCAPE == conf.orientation) { View contentView = decorView.findViewById(android.R.id.content); height = Math.abs(point.x - contentView.getWidth()); } else { Rect rect = new Rect(); decorView.getWindowVisibleDisplayFrame(rect); height = Math.abs(rect.bottom - point.y); } return height; }

获取状态栏高度方法:

/** * 获得状态栏的高度(无关 android:theme 获取状态栏高度) * * @return */ public static int getStatusHeight(Context context) { try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString()); return context.getResources().getDimensionPixelSize(height); } catch (Exception e) { } return 0; }
最新回复(0)