当前位置: 首页 > article >正文

Android获取状态栏、导航栏的高度

Android获取状态栏的高度:

方法一:通过资源名称获取, getDimensionPixelSize,获取系统中"status_bar_height"的值,方法如下:

Java:

public static int getStatusBarHeight(Context context) {
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

Kotlin:

    fun getStatusBarHeight(context: Context): Int {
        var result = 0
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }
方法二:添加布局后获取

Kotlin:

 ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
方法三:通过setOnApplyWindowInsetsListener
    fun getStatusBarHeightWithListener(activity: Activity, callback: (Int) -> Unit) {
        activity.window.decorView.setOnApplyWindowInsetsListener { v, insets ->
            val statusBarHeight = insets.systemWindowInsetTop
            callback(statusBarHeight)
            // 返回insets以允许其他监听器继续接收
            insets
        }
        // 触发布局以尽快调用监听器
        activity.window.decorView.requestLayout()
    }

调用:

    getStatusBarHeightWithListener(this) { statusBarHeight ->
            // 使用statusBarHeight
            LogUtils.i("getStatusBarHeightWithListener:${statusBarHeight}")
        }
方法四:通过 WindowInsets 获取

这种方法需要 API 20 (Android 4.4W) 以上,但在较新版本的 Android(API 21及以上)中更为准确。

fun getStatusBarHeight(activity: Activity): Int {
    val windowInsets = activity.window.decorView.rootWindowInsets
    return windowInsets?.systemWindowInsetTop ?: 0
}

注意:在 Android 11(API 30)及以上版本可以使用 WindowInsetsCompat 进行更兼容性友好的操作。

import androidx.core.view.WindowInsetsCompat
import androidx.core.view.ViewCompat

fun getStatusBarHeight(activity: Activity): Int {
    val insets = ViewCompat.getRootWindowInsets(activity.window.decorView)
    return insets?.systemWindowInsetTop ?: 0
}

这种方法直接获取会返回0,需要布局加载完成或者view.post中调用:

    mViewBinding.main.post {
              LogUtils.i("actionBarHeight:${getStatusBarHeight(this)}")
        }
方法五:使用固定值24dp(不推荐)

在Android 9.0 frameworks/base/core/res/res/中有如下

    <!-- Height of the status bar -->
    <dimen name="status_bar_height">@dimen/status_bar_height_portrait</dimen>
    <!-- Height of the status bar in portrait -->
    <dimen name="status_bar_height_portrait">24dp</dimen>
    <!-- Height of the status bar in landscape -->
    <dimen name="status_bar_height_landscape">@dimen/status_bar_height_portrait</dimen>

可以看到status_bar_height只有一个定值24dp,因此可以直接使用

Android 9.0的frameworks/base/core/res/res目录源码:https://android.googlesource.com/platform/frameworks/base/+archive/refs/heads/pie-release-2/core/res/res.tar.gz
同理 navigation_bar_height 可以直接用48dp

Android获取导航栏的高度:

fun getNavigationBarHeight(context: Context): Int {
    var result = 0
    val resourceId = context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
    if (resourceId > 0) {
        result = context.resources.getDimensionPixelSize(resourceId)
    }
    return result
}


http://www.kler.cn/a/417623.html

相关文章:

  • Three.js 后期处理(Post-Processing)详解
  • Kubernetes 中 BGP 与二层网络的较量:究竟孰轻孰重?
  • Beans模块之工厂模块注解模块CustomAutowireConfigurer
  • 1-ET框架开发环境与demo运行
  • 逻辑回归原理
  • uniapp小程序自定义中间凸起样式底部tabbar
  • 【2025最新计算机毕业设计】基于SpringBoot+Vue文化创意展示与交流平台【提供源码+答辩PPT+文档+项目部署】
  • YOLO系列论文综述(从YOLOv1到YOLOv11)【第14篇:YOLOv11——在速度和准确性方面具有无与伦比的性能】
  • 动捕 动作捕捉学习笔记
  • C++内存对齐
  • 【从零开始的LeetCode-算法】263. 丑数
  • python全栈开发《67.不同数据类型间的转换:列表集合元组的转换》
  • 【Leecode】Leecode刷题之路第66天之加一
  • Maven CMD命令
  • 共享售卖机语音芯片方案选型:WTN6020引领智能化交互新风尚
  • 【Ant Design Pro】1. config 配置
  • 实战ansible-playbook:Ansible Vault加密敏感数据(三)
  • 田忌赛马五局三胜问题matlab代码
  • 大模型训练核心技术RLHF
  • 关于扩散方程的解
  • 命令行应用开发初学者指南:脚手架篇、UI 库和交互工具
  • 【AI】Jetson Nano烧写SD卡镜像:Ubuntu20.04
  • Vue 2.0->3.0学习笔记(Vue 3 (五)- 新的组件)
  • 本地学习axios源码-如何在本地打印axios里面的信息
  • 如何构建一个可扩展、全球可访问的 GenAI 架构?
  • 回调函数知识点