边到边的 android15 导航栏遮挡内容适配
作者: yandongletter创建于 2025年8月5日更新于 2026年2月27日
private void registerActivityLifecycleCallbacks() { registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { if (activity is PictureSelectorSupporterActivity || activity is PictureSelectorTransparentActivity || ... ) { //your idea NavigationBarUtil.INSTANCE.applyNavigationBarPadding(activity) } } }) }
object NavigationBarUtil {
val SUPPORT_EDGE_TO_EDGE_VERSION = Build.VERSION_CODES.VANILLA_ICE_CREAM // 15+
@JvmField
val isEdgeToEdgeSupported = Build.VERSION.SDK_INT >= SUPPORT_EDGE_TO_EDGE_VERSION
/**
* Adapt the bottom navigation bar: add padding to the bottom navigation bar to the root layout.
*/
fun applyNavigationBarPadding(activity: Activity) {
if (isEdgeToEdgeSupported) {
// Edge-to-Edge mode adaptation
val rootView = activity.window.decorView.findViewById<View>(android.R.id.content)
if (rootView != null) {
applyNavigationBarInsets(rootView)
}
}
}
/**
* Add a padding of the bottom navigation bar height to the view.
*/
fun applyNavigationBarInsets(view: View) {
val initialPaddingBottom = view.paddingBottom
ViewCompat.setOnApplyWindowInsetsListener(view) { v: View, insets: WindowInsetsCompat ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(
v.paddingLeft,
v.paddingTop,
v.paddingRight,
initialPaddingBottom + systemBars.bottom
)
insets
}
}
}
内容来源: LuckSiege/PictureSelector