使用下面的Kotlin代码获取操作栏,状态栏和导航栏的高度(以像素为单位)
/* Height of Action Bar*/
var mActionBarHeight = 0
val mAttributes = getTheme().obtainStyledAttributes(
intArrayOf(android.R.attr.actionBarSize)
)
mActionBarHeight = mAttributes.getDimension(0, 0f).toInt()
mAttributes.recycle()
/* Height of Status Bar */
var mStatusBarHeight = 0
val mResId = resources.getIdentifier("status_bar_height", "dimen", "android")
if (mResId > 0) {
mStatusBarHeight = resources.getDimensionPixelSize(mResId)
}
/* Height of Navigation Bar */
var mNavigationBarHeight = 0
val mResId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (mResId > 0) {
mNavigationBarHeight = resources.getDimensionPixelSize(mResId)
}