Why would I want to fitsSystemWindows?

Overview

SystemUiTraining

">


   


    
    

   

android:fitsSystemWindows="false" 结果 image1

android:fitsSystemWindows="true" 结果 image2

我们为什么要用fitsSystemWindows?

System windows 指的就是屏幕上status bar、 navigation bar等系统控件所占据的部分。

绝大多数情况下,你都不需要理会status bar或者navigation bar 下面的空间,不过你需要注意不能让你的交互控件(比如Button)藏在status bar 或者 navigation bar下面。而android:fitsSystemWindows="true"的默认行为正好解决了这种情况,这个属性的作用就是通过设置View的padding,使得应用的content部分——Activity中setContentView()中传入的就是content——不会与system window重叠。

还有一些事情需要注意:

  • fitsSystemWindows 需要被设置给根View——这个属性可以被设置给任意View,但是只有根View(content部分的根)外面才是SystemWindow,所以只有设置给根View才有用。
  • Insets始终相对于全屏幕——Insets即边框,它决定着整个Window的边界。对Insets设置padding的时候,这个padding始终是相对于全屏幕的。因为Insets的生成在View layout之前就已经完成了,所以系统对于View长什么样一无所知。
  • 其它padding将通通被覆盖。需要注意,如果你对一个View设置了android:fitsSystemWindows="true",那么你对该View设置的其他padding将通通无效。

在绝大多数情况下,默认情况就已经够用了。比如一个全屏的视屏播放器。如果你不想被ActionBar 或者其他System View遮住的话,那么在MatchParent的ViewGroup上设置fitsSystemWindows="true"即可。

或者,也许你希望你的RecyclerView能够在透明的navigation bar 下面滚动。那么只需将android:fitsSystemWindows="true" android:clipToPadding="false"同时使用即可, 滚动的内容会绘制在navigation bar下面,同时当滚动到最下面的时候,最后一个item下面依旧会有padding,使其可以滚到navigation bar上方(而不是在navigation bar下面滚不上来!)。

译者注:clipToPaddingViewGroup的属性。这个属性定义了是否允许ViewGroup在padding中绘制,该值默认为true,即不允许。

自定义 fitsSystemWindows

但是默认毕竟只是默认。 在KITKAT及以下的版本,你的自定义View能够通过覆盖fitsSystemWindows() : boolean函数,来增加自定义行为。如果返回true,意味着你已经占据了整个Insets,如果返回false,意味着给其他的View依旧留有机会。

而在Lollipop以及更高的版本,我们提供了一些新的API,使得自定义这个行为更加的方便。与之前的版本不同,现在你只需要覆盖OnApplyWindowInsets()方法,该方法允许View消耗它想消耗的任意空间(Insets),同时也能够为子方法,调用dispatchApplyWindowInsets()

更妙的是,利用新的API,你甚至不需要拓展View类,你可以使用ViewCompat.setOnApplyWindowInsetsListener(),这个方法优先于View.onApplyWindowInsets()调用。ViewCompat 同时也提供了 onApplyWindowInsets()dispatchApplyWindowInsets() 的兼容版本,无需冗长的版本判断。

自定义fitsSystemWindows例子

绝大多数基本的layouts(FrameLayout)都是使用默认的行为,然而依然有一部分layouts已经使用了自定义fitsSystemWindow来实现自定义的功能。

navigation drawer就是一个例子,它需要充满整个屏幕,绘制在透明的status bar下面。

如上图所示,DrawerLayout使用了fitsSystemwindows,他需要让它的子View依旧保持默认行为,即不被actionbar或其他system window遮住,同时依照Material Design的定义,又需要在透明的statusbar下面进行绘制(默认是你在theme中设置的colorPrimaryDark颜色)

你会注意到,在Lollipop及以上版本,DrawerLayout为每一个子View调用了dispatchApplyWindowInsets(),使每一个子View都收到fitsSystemWindows。这与默认行为完全不同,默认行为会使得根View消耗所有的insets,同时子View们永远不会收到fitsSystemWindows

CoordinatorLayout也利用了这一特性,使得其子View有机会截断并对fitsSystemWindows做出自己的反应。同时,它也利用fitsSystemWindows这一flag看其是否需要在statusbar的下方绘制。

同样的,CollapsingToolbarLayoutfitsSystemWindows 什么时候把变小的View放在什么地方。

如果你对这些Design Library里的东西感兴趣,请查看Cheesesquare Sample

积极使用系统,而不是老想着Hack

有一件事需要始终牢记,这个属性毕竟不是fitsStatusBar或者fitsNavigationBar。不管是尺寸还是位置,在不同版本间,系统控件都有很大的差距。

但是尽管放心,无论在什么平台上,fitsSystemWindows都会影响Insets,使你的content和system ui不会重叠——除非你自定义这一行为。

You might also like...
You don’t want your apps look and feel boring, do you? Add some bubbles!
You don’t want your apps look and feel boring, do you? Add some bubbles!

#BubbleAnimationLayout Say hello to Bubble Animation Layout for Android by Cleveroad You don’t want your apps look and feel boring, do you? Add some b

Kotlin extensions for Moshi, Make every thing you want with Moshi in just one line.

Kotlin extensions for Moshi, Make every thing with square / Moshi in one line.

A tool that enables advanced features through adb installing and uninstalling apps like wildcards and multi device support. Useful if you want to clean your test device from all company apks or install a lot of apks in one go.  Written in Java so it should run on your platform. Find your ideal fitness partners according to your preferences and interact with them whenever you want! All this with no hassle, because there's FitMate! Take timed challenges updated daily, read blogs related to health, and be a part of numerous communities too! During covid times, partner with your FitMate to achieve your fitness goals at home. A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

A movie application where you can search a movie that you want and see the detail of movie
A movie application where you can search a movie that you want and see the detail of movie

MovieApp This is a simple movie application where you can search a movie that you want and see the detail of movie Technologies Hilt (Dependency Injec

A digital wellbeing Android app that helps minimise distractions when you don't want them
A digital wellbeing Android app that helps minimise distractions when you don't want them

Bundel is a digital wellbeing Android app, helping you to focus by grouping up notifications and only releasing them in batches, at set times. This minimises context switching and improves productivity, while decreasing the impulse to continuously check your phone.

Workout Journal is a mobile app based on Multi-Module and Clean Architecture for those who want to track their progress over a workout and a calendar period.
Workout Journal is a mobile app based on Multi-Module and Clean Architecture for those who want to track their progress over a workout and a calendar period.

Workout-Journal Workout Journal is a mobile app for those who want to track their progress over a workout and a calendar period. The app allows you to

A simple and minimal app to track how long you've been sober from anything you want.
A simple and minimal app to track how long you've been sober from anything you want.

Sobriety A simple and minimal app to track how long you've been sober from anything you want. I mostly designed this out of spite for "I Am Sober", wh

This app works as a simple replacement for SpongeAuth for those who want to use Discourse as the main SSO auth provider.

PowerNukkit Ore to Discourse Auth Gateway This app works as a simple replacement for SpongeAuth for those who want to use Discourse as the main SSO au

Introduction your app to the user , Easy to use and set Items as you want

Introduction App This lib helps to introduce the App-by view page based on Kotlin. Features Easy Set up Items: Title, Describe, Background, Buttons Ap

Xposed module which will set location where you want without effect mock location.
Xposed module which will set location where you want without effect mock location.

GPS Setter Support/Discussion: XDA thread As most of GPS spoof app not working anymore coz some are old and some are not proper implement with current

This project created just for help developer who want to and ability of read VISA, UNION PAY, HUMO, ATTO and some other cards data read.

If you enjoy my content, please consider supporting what I do. Thank you. By me a Coffee To get a Git project into your build: Step 1. Add the JitPack

Owner
Seachal
学习ing
Seachal
A set of web-based tools for generating graphics and other assets that would eventually be in an Android application's res/ directory.

Android Asset Studio Open the Android Asset Studio See the older version if you're having trouble with the new version A web-based set of tools for ge

Roman Nurik 6.3k Dec 31, 2022
An MVI project setup using Jetpack compose. This would be a good place to start learning Model View Intent (MVI) architecture for Android.

Compose-MVI An MVI project setup using Jetpack compose. This would be a good place to start learning Model View Intent (MVI) architecture for Android.

null 6 Jul 28, 2022
An ActivityScenario extension and a Junit4 TestRule to simulate changing the font size on a device/emulator, as it would be done by going to Settings > display > Font size

FontSizeActivityScenario and FontSizeTestRule An ActivityScenario and a Junit4 TestRule to be used together with its org.junit.runners.Parameterized.

Sergio Sastre Flórez 19 Jan 6, 2022
v9 is an Android library that provides the ability to dynamically resize Path objects as you would 9-patch bitmaps

v9 is an Android library that provides the ability to dynamically resize Path objects as you would 9-patch bitmaps. In the example below, the chat bubble is dynamically resized while preserving the corners:

Romain Guy 304 Dec 27, 2022
If you had one month to visit the best places in Kenya, where would you go?

VisitKenya If you had one month to visit the best places in Kenya, where would you go? This app just shows you sample places where you could visit, wi

Alan Derich 1 Oct 20, 2022
Android fillable progress view working with SVG paths. This is a nice option too if you want to create an interesting branding logo for your app. Based on the iOS project: https://github.com/poolqf/FillableLoaders

Android FillableLoaders Android Open Source library providing an interesting fillable progress view working with SVG paths. This is a nice option too

Jorge Castillo 2k Jan 1, 2023
💪 Rich Android Path. 🤡 Draw as you want. 🎉 Animate much as you can.

?? Rich Android Path. ?? Draw as you want. ?? Animate much as you can. Download sample app: Features Full Animation Control on Paths and VectorDrawabl

Ahmed Tarek 2.3k Dec 20, 2022
Android port of Simon Tatham's Puzzles. *git replace in use* - you might want to {{ git fetch origin 'refs/replace/*:refs/replace/*' }}

This is the README accompanying the source code to Simon Tatham's puzzle collection. The collection's web site is at <https://www.chiark.greenend.org.

Chris Boyle 470 Dec 22, 2022
You don’t want your apps look and feel boring, do you? Add some bubbles!

#BubbleAnimationLayout Say hello to Bubble Animation Layout for Android by Cleveroad You don’t want your apps look and feel boring, do you? Add some b

Cleveroad 576 Nov 23, 2022