fix: status bar system ui

This commit is contained in:
lisonge 2024-06-17 01:19:30 +08:00
parent b6a05c0b92
commit 24fe493d11

View File

@ -1,10 +1,14 @@
package li.songe.gkd.composition package li.songe.gkd.composition
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.blankj.utilcode.util.BarUtils
open class CompositionActivity( open class CompositionActivity(
private val block: CompositionActivity.(Bundle?) -> Unit, private val block: CompositionActivity.(Bundle?) -> Unit,
@ -30,6 +34,7 @@ open class CompositionActivity(
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen() installSplashScreen()
enableEdgeToEdge() enableEdgeToEdge()
fixTopPadding()
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
block(savedInstanceState) block(savedInstanceState)
} }
@ -42,5 +47,25 @@ open class CompositionActivity(
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
configurationChangedHooks.forEach { f -> f(newConfig) } configurationChangedHooks.forEach { f -> f(newConfig) }
} }
}
} fun ComponentActivity.fixTopPadding() {
// 当调用系统分享时, 会导致状态栏区域消失, 应用整体上移, 设置一个 top padding 保证不上移
var tempTop: Int? = null
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, windowInsets ->
view.setBackgroundColor(Color.TRANSPARENT)
val statusBars = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars())
if (statusBars.top == 0) {
view.setPadding(
statusBars.left,
tempTop ?: BarUtils.getStatusBarHeight(),
statusBars.right,
statusBars.bottom
)
} else {
tempTop = statusBars.top
view.setPadding(statusBars.left, 0, statusBars.right, statusBars.bottom)
}
ViewCompat.onApplyWindowInsets(view, windowInsets)
}
}