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
}
相关推荐
Tom4i8 小时前
【网络优化】Android 如何监听系统网络连接成功
android·网络
FrameNotWork8 小时前
HarmonyOS 与 Android 架构对比:从“写页面”到“设计系统”的差异
android·架构·harmonyos
TAEHENGV9 小时前
基本设置模块 Cordova 与 OpenHarmony 混合开发实战
android·java·数据库
屏息11 小时前
Android 低延迟流媒体播放器实战:基于 FFmpeg 6.1.1 的 RTSP/RTMP 解决方案
android
TeleostNaCl12 小时前
Kodi | 如何使用 ADB 无 root 备份 Android 版本 Kodi 的数据并导入到另一台设备中
android·经验分享·adb·电视盒子·智能电视·tv·智能tv
csj5013 小时前
安卓基础之《(10)—中级控件(4)对话框》
android
nono牛13 小时前
Android.bp 配置文件详解---gatekeeperd
android
来来走走13 小时前
Android开发(kotlin) 开发一个简单天气应用
android·kotlin
SweetCode14 小时前
汉诺塔问题
android·java·数据库
音视频牛哥15 小时前
Android 端构建高性能 RTSP 转 RTMP|轻量级RTSP服务 网关:透传与二次编码深度实践
android·音视频·大牛直播sdk·rtsp转rtmp推送·rtsp转发到rtsp服务器·rtsp转rtmp二次编码推送·rtsp二次编码加水印