Created
June 5, 2019 09:24
-
-
Save jusonqiu/7025f8e05c5fa1a82fd7b4c0d82a13ab to your computer and use it in GitHub Desktop.
设置android status bar 沉浸式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun setTranslucentStatus() { | |
// 5.0以上系统状态栏透明 | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
//设置沉浸状态 | |
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) | |
//设置UI渗入,StatusBar ICON 颜色 | |
var options = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or SYSTEM_UI_FLAG_LAYOUT_STABLE | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ | |
options = options or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR //dark icon | |
} | |
window.decorView.systemUiVisibility = options | |
//跟系統表示要渲染 system bar 背景 | |
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) | |
//透明StatusBar背景 | |
window.statusBarColor = Color.TRANSPARENT | |
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) | |
} | |
} | |
private fun getStatusBarHeight(): Int { | |
var result = 0 | |
val resourceId = resources | |
.getIdentifier("status_bar_height", "dimen", "android") | |
if (resourceId > 0) { | |
result = resources.getDimensionPixelSize(resourceId) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment