[翻译]深入理解fitsSystemWindows

翻译自《Why would I want to fitsSystemWindows?》 原作者:Ian Lake

说到fitSystemWindows,那什么是System Windows呢?System Windows一般而言就是指的由SystemUI在屏幕绘制的组件,一般而言包含非交互的UI(如:Status Bar)、交互UI(如:Navigation Bar)。

大部分情况下,你的App应该不需要扩展到Status bar或者Navigation Bar下面,不过如果需要,你需要确保交互元素(比如Button)不要被遮挡在它们下面。这也就是android:fitsSystemWindows=“true”属性的作用:自动设置View的padding从而确保内容不会被System windows组件遮挡。

需要记住的是:

  • fitsSystemWindows is applied depth first– ordering matters: it’s the first View that consumes the insets that makes a difference
  • Insets are always relative to the full window– insets may be applied even before layout happens, so don’t assume the default behavior knows anything about the position of a View when applying its padding
  • Any other padding you’ve set is overwritten –you’ll note that paddingLeft/paddingTop/etc is ineffective if you are using android:fitsSystemWindows=”true” on the same View

另外,在全屏视频播放等众多例子中,这些就足够了。出血View不设置该属性,其他全屏的ViewGroup设置android:fitsSystemWindows=”true”