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

这篇具有很好参考价值的文章主要介绍了【Android】获取导航栏、状态栏高度。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

获取状态栏高度

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

<dimen name="toast_y_offset">24dp</dimen>
<!-- Height of the status bar -->
<dimen name="status_bar_height">@dimen/status_bar_height_portrait</dimen>
<!-- Height of the status bar in portrait. The height should be
     Max((status bar content height + waterfall top size), top cutout size) -->
<dimen name="status_bar_height_portrait">24dp</dimen>
<!-- Height of the status bar in landscape. The height should be
     Max((status bar content height + waterfall top size), top cutout size) -->
<dimen name="status_bar_height_landscape">@dimen/status_bar_height_portrait</dimen>
<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
<!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->
<dimen name="navigation_bar_height_landscape">48dp</dimen>
<!-- Width of the navigation bar when it is placed vertically on the screen -->
<dimen name="navigation_bar_width">48dp</dimen>

通过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更新背景色的方法,还有如下位置:
android 获取状态栏高度,android,android

#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 << 0;
static final int STATUS_BARS = FIRST;
static final int NAVIGATION_BARS = 1 << 1;
static final int CAPTION_BAR = 1 << 2;

static final int IME = 1 << 3;

static final int SYSTEM_GESTURES = 1 << 4;
static final int MANDATORY_SYSTEM_GESTURES = 1 << 5;
static final int TAPPABLE_ELEMENT = 1 << 6;

static final int DISPLAY_CUTOUT = 1 << 7;

static final int LAST = 1 << 8;
static final int SIZE = 9;
static final int WINDOW_DECOR = LAST;

可以向DecorView添加监听,处理WindowInsets

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    return insets;
});

所以,可以在这里获取导航栏和状态栏的高度:

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    int statuBarHeight = insets.getStableInsetTop();
    int navigationBarHeight = insets.getStableInsetBottom();
    return insets;
});

也可以修改导航栏和状态栏的高度(好像没有应用场景):文章来源地址https://www.toymoban.com/news/detail-520367.html

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    int statuBarHeight = 0;
    int navigationBarHeight = 0;
    return insets.replaceSystemWindowInsets(0,statuBarHeight,0,navigationBarHeight);
;
});

到了这里,关于【Android】获取导航栏、状态栏高度的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序如何实现自定义导航栏、导航栏手机适配(获取导航栏、状态栏高度和胶囊位置)以及踩过的坑

    背景:不用官方默认的导航栏,想用自己自定义的 实现效果: :顶部状态栏、顶部导航栏、顶部状态导航栏、胶囊 原理: 自定义导航栏无非就是求得导航栏高度,并让内容容器垂直方向居中于导航栏高度 1.获取手机系统状态栏高度 2.获取胶囊位置(包括高度) 3.求得

    2024年03月12日
    浏览(52)
  • Android获取文本的宽度和高度

    方法一:先绘制文本所在的矩形区域,再获取矩形区域的宽度 上述方法由于矩形边框紧贴文字,所有没有多余的空间。 方法二:通过Paint的 measureText 方法直接测量文本宽度 此方法计算出的宽度会加上开始和结尾的空间,这个空间就是文字和文字之间的空间,为了美观而存在

    2024年02月09日
    浏览(41)
  • Android WindowInsetsController 设置状态栏、导航栏

    有 hide() ,也有 show() 在更早的版本中,使用 ViewCompat.getWindowInsetsController() 获取 WindowInsetsControllerCompat 实例 而现在推荐使用 WindowCompat.etInsetsController() 获取 WindowInsetsControllerCompat 实例 底部的三个按键就是导航栏 (navigation bar): back / home / recent 。 高版本系统,recent,可能没有图

    2024年02月15日
    浏览(34)
  • android 12.0状态栏高度为0时,系统全局手势失效的解决方案

    在12.0的framework 系统全局手势事件也是系统非常重要的功能,但是当隐藏状态栏, 当把状态栏高度设置为0时,这时全局手势事件失效,这就要从系统手势滑动流程来分析 看怎么样实现系统手势功能的,然后根据功能做修改

    2024年02月07日
    浏览(25)
  • Android 显示、隐藏状态栏和导航栏

    控制状态栏显示,Activity的主题中配置全屏属性 控制状态栏显示,在setContentView之前设置全屏的flag 复制代码 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 控制状态栏显示,在任何位置通过添加和移除全屏的flag 控制状态栏和导航栏显示,

    2024年02月08日
    浏览(30)
  • Android隐藏导航栏和状态栏的方法

    一。去除状态栏 以下是Android去除状态栏的代码示例: 1. 在Activity的onCreate()方法中添加以下代码: getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 2. 在AndroidManifest.xml文件中的Activity节点中添加以下属性: android:theme=\\\"@android:style/Theme.NoTitleB

    2024年02月16日
    浏览(24)
  • 史上最完美的Android沉浸式状态导航栏攻略

    最近我在小破站开发一款新App,叫 高能链 。我是一个完美主义者,所以不管对架构还是UI,我都是比较抠细节的,在状态栏和导航栏沉浸式这一块,我还是踩了挺多坑,费了挺多精力的。这次我将我踩坑,适配各机型总结出来的史上最完美的Android沉浸式状态导航栏攻略分享

    2023年04月26日
    浏览(28)
  • uniapp 小程序自定义导航栏计算状态栏(顶部)与导航栏(胶囊)高度

    uni.getMenuButtonBoundingClientRect() 参考链接 uni.getSystemInfo()参考链接

    2024年02月11日
    浏览(58)
  • Android 标题栏、状态栏、系统栏、导航栏、应用栏及各个位置的颜色设置

    如上图,可以看到,有状态栏(status bar)、标题栏(action bar, toolbar)、导航栏(navigation bar) 等, 状态栏 (status bar):是指手机最顶上,显示中国移动、安全卫士、电量、网速等等,在手机的顶部。下拉就会出现通知栏。 标题栏就是指action bar/toolbar,app程序最上边的titlebar。关于acti

    2023年04月16日
    浏览(26)
  • Android全屏弹出Dialog显示状态栏和导航栏的问题及解决方案

    在移动端开发中,有时候我们需要在Android应用中弹出一个全屏的Dialog。然而,当我们尝试实现这样的Dialog时,可能会遇到一个问题:状态栏和导航栏在全屏Dialog中仍然可见,这可能会影响用户体验。本文将介绍如何解决这个问题,并提供相应的源代码。 问题描述: 当我们使

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包