compose设置透明状态栏

509 阅读1分钟

摘录至github上一个仿写网易云的项目

/**
 * Created by ssk on 2022/4/17.
 */

/**
 * 设置为沉浸式状态栏
 */
fun Activity.transparentStatusBar() {
    WindowCompat.setDecorFitsSystemWindows(window, false)
}

/**
 * 状态栏反色
 */
fun Activity.setAndroidNativeLightStatusBar() {
    val decor = window.decorView
    val isDark = resources.configuration.uiMode == 0x21
    if (!isDark) {
        decor.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    } else {
        decor.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }
}
/**
 * @author zengyifeng
 * @date createDate:2023-03-21
 * @brief description  设置状态栏透明,图标白色
 */

@Composable
fun setFixSystemBarsColor() {
    val sysUiController = rememberSystemUiController()
	sysUiController.setSystemBarsColor(Color.Transparent, false)
}

依赖
    implementation "com.google.accompanist:accompanist-systemuicontroller:0.15.0"

使用

在setContent前使用

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        transparentStatusBar()
        setAndroidNativeLightStatusBar()
        setContent {
                setFixSystemBarsColor()
		...
		}