- 首页 > 睿知 > it技术 > >
android|android kotlin Dimension
androidjava开发语言框架
val Float.dp: Float// [xxhdpi](360 -> 1080)
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics
)val Int.dp: Int
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), Resources.getSystem().displayMetrics
).toInt()val Float.sp: Float// [xxhdpi](360 -> 1080)
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics
)val Int.sp: Int
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, this.toFloat(), Resources.getSystem().displayMetrics
).toInt()/**
* 状态栏的高度
*/
inline val Context.statusBarHeight: Int
get() {
var result = 0
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
if (resourceId > 0) {
result = runCatching { resources.getDimensionPixelSize(resourceId) }.getOrDefault(0)
}if (result == 0) {
result = 24.dp
}
return result
}val Context.screenWidthPx: Int
get() = resources.displayMetrics.widthPixelsval Context.screenHeightPx: Int
get() = resources.displayMetrics.heightPixels
推荐阅读