简体   繁体   中英

How to get statusbar insets for android app?

I'm trying to make my android app "edge to edge" (make statusbar and navbar translucent). I've read many articles online about this topic but since so many methods and classes are deprecated now, I tried to write it using new ways. The app runs Ok on my physical Samsung phone running android 11, but it gives me runtime error for getting insets while launching the app on my android studio emulator running android 10. Take a look at the code and error:

code (onCreateView() in mainFragment.kt):

binding.appbar.setOnApplyWindowInsetsListener { view, windowInsets ->
  val insets = windowInsets.getInsets(WindowInsets.Type.statusBars())
  view.updatePadding(top = insets.top)
  windowInsets
}

error:

java.lang.NoSuchMethodError: No static method statusBars()I in class Landroid/view/WindowInsets$Type; or its super classes (declaration of 'android.view.WindowInsets$Type' appears in /system/framework/framework.jar!classes3.dex)
.MainFragment$onCreateView$1.onApplyWindowInsets(MainFragment.kt:44)

update: There is also no problem with an emulator running android 11, Now I think I had to do the job differently for lower APIs? but how exactly?!

Try to use the same method that you're using from the androidX lib.

ViewCompat.setOnApplyWindowInsetsListener(view, listener)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM