【Android】获取导航栏、状态栏高度

您所在的位置:网站首页 安卓底部导航栏高度怎么调 【Android】获取导航栏、状态栏高度

【Android】获取导航栏、状态栏高度

2024-04-02 21:54| 来源: 网络整理| 查看: 265

获取状态栏高度 public static int getStatusBarHeight(Context context) { int result = 0; int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = context.getResources().getDimensionPixelSize(resourceId); } return result; }

或者

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> { int statuBarHeight = insets.getStableInsetTop(); return insets; }); 获取导航栏高度 public static int getNavigationBarHeight(Context context) { int result = 0; int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { result = context.getResources().getDimensionPixelSize(resourceId); } return result; }

或者

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> { int navigationBarHeight = insets.getStableInsetBottom(); return insets; }); 其他数据

系统的各种数据定义位于SDK的xml文件中:

android-30/data/res/values/dimens.xml

24dp @dimen/status_bar_height_portrait 24dp @dimen/status_bar_height_portrait 48dp 48dp 48dp

通过key可以获取对应的值。

修改导航栏、状态栏背景色 getWindow().setStatusBarColor(Color.RED); getWindow().setNavigationBarColor(Color.RED); 源码

导航栏和状态栏源码相似

#PhoneWindow @Override public void setStatusBarColor(int color) { mStatusBarColor = color; mForcedStatusBarColor = true; if (mDecor != null) { mDecor.updateColorViews(null, false /* animate */); } } #DecorView public static final ColorViewAttributes STATUS_BAR_COLOR_VIEW_ATTRIBUTES = new ColorViewAttributes(SYSTEM_UI_FLAG_FULLSCREEN, FLAG_TRANSLUCENT_STATUS, Gravity.TOP, Gravity.LEFT, Gravity.RIGHT, Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME, com.android.internal.R.id.statusBarBackground, FLAG_FULLSCREEN, ITYPE_STATUS_BAR); public static final ColorViewAttributes NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES = new ColorViewAttributes( SYSTEM_UI_FLAG_HIDE_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION, Gravity.BOTTOM, Gravity.RIGHT, Gravity.LEFT, Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME, com.android.internal.R.id.navigationBarBackground, 0 , ITYPE_NAVIGATION_BAR); private final ColorViewState mStatusColorViewState = new ColorViewState(STATUS_BAR_COLOR_VIEW_ATTRIBUTES); private final ColorViewState mNavigationColorViewState = new ColorViewState(NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES); WindowInsets updateColorViews(WindowInsets insets, boolean animate) { ... updateColorViewInt(mNavigationColorViewState, sysUiVisibility, calculateNavigationBarColor(), mWindow.mNavigationBarDividerColor, navBarSize, navBarToRightEdge || navBarToLeftEdge, navBarToLeftEdge, 0 /* sideInset */, animate && !disallowAnimate, mForceWindowDrawsBarBackgrounds, controller); ... updateColorViewInt(mStatusColorViewState, sysUiVisibility, calculateStatusBarColor(), 0, mLastTopInset, false /* matchVertical */, statusBarNeedsLeftInset, statusBarSideInset, animate && !disallowAnimate, mForceWindowDrawsBarBackgrounds, controller); }

调用updateColorViews更新背景色的方法,还有如下位置: 在这里插入图片描述

#ViewRootImpl private void performTraversals() { dispatchApplyInsets(host); } public void dispatchApplyInsets(View host) { mApplyInsetsRequested = false; WindowInsets insets = getWindowInsets(true /* forceConstruct */); host.dispatchApplyWindowInsets(insets); ... } #View public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { ... return onApplyWindowInsets(insets); ... } #DecorView @Override public WindowInsets onApplyWindowInsets(WindowInsets insets) { ... insets = updateColorViews(insets, true); ... return insets; } 关于WindowInsets

WindowInsets可以翻译为窗口附加物,一般是指一个界面中,不由开发者直接控制的部分,例如:状态栏、底部的导航栏等等。具体的类型在Type中有定义:

#WindowInsets.Type static final int FIRST = 1


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3