欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > Android获取状态栏、导航栏的高度

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

2025/9/21 4:57:59 来源:https://blog.csdn.net/Billy_Zuo/article/details/144082641  浏览:    关键词: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 = 0val 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.systemWindowInsetTopcallback(statusBarHeight)// 返回insets以允许其他监听器继续接收insets}// 触发布局以尽快调用监听器activity.window.decorView.requestLayout()}

调用:

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

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

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

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

import androidx.core.view.WindowInsetsCompat
import androidx.core.view.ViewCompatfun 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 = 0val resourceId = context.resources.getIdentifier("navigation_bar_height", "dimen", "android")if (resourceId > 0) {result = context.resources.getDimensionPixelSize(resourceId)}return result
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词