对于android:fitssystemwindows这个属性你是否感觉又熟悉又陌生呢?
熟悉是因为大概知道它可以用来实现沉浸式状态栏的效果,陌生是因为对它好像又不够了解,这个属性经常时灵时不灵的。
其实对于android:fitssystemwindows属性我也是一知半解,包括我在写《第一行代码》的时候对这部分知识的讲解也算不上精准。但是由于当时的理解对于我来说已经够用了,所以也就没再花时间继续深入研究。
而最近因为工作的原因,我又碰上了android:fitssystemwindows这个属性,并且我之前的那些知识储备已经不够用了。所以这次趁着这个机会,我把这部分知识又重新学习了一遍,并整理成一篇文章分享给大家。
我们都不会无缘无故去接触一个属性。我相信用到android:fitssystemwindows的朋友基本都是为了去实现沉浸式状态栏效果的。
这里我先解释一下什么是沉浸式状态栏效果。
android手机顶部用于显示各种通知和状态信息的这个栏叫做状态栏。
通常情况下,我们应用程序的内容都是显示在状态栏下方的。但有时为了实现更好的视觉效果,我们希望将应用程序的内容延伸到状态栏的背后,这种就可以称之为沉浸式状态栏。
那么借助android:fitssystemwindows属性是如何实现沉浸式状态栏效果的呢?这个属性为什么又总是时灵时不灵呢?接下来我们就来一步步学习和揭秘。
我相信按照绝大多数人的美好设想,android:fitssystemwindows属性就应该像是一个开关一样,设置成true就可以打开沉浸式状态栏效果,设置成false就可以关闭沉浸式状态栏效果。但现实并非如此。
下面我们通过代码示例来演示一下。首先为了验证沉浸式状态栏的效果,需要将系统的状态栏改成透明色,代码如下所示:
class mainactivity : appcompatactivity() { override fun oncreate(savedinstancestate: bundle?) { super.oncreate(savedinstancestate) setcontentview(r.layout.activity_main) window.statusbarcolor = color.transparent } }
接下来,我们给activity_main.xml的根布局加上android:fitssystemwindows属性,并且给该布局设置了一个背景色用于观察效果:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff66ff" android:fitssystemwindows="true"> </framelayout>
运行一下代码,效果如下图所示:
通过布局的背景色我们可以看出,该布局的内容并没有延伸到系统状态栏的背后。也就是说,即使设置了android:fitssystemwindows属性,我们也没有实现沉浸式状态栏效果。
但是不要着急,接下我们只需要做出一点小修改,如下所示:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff66ff" android:fitssystemwindows="true"> </androidx.coordinatorlayout.widget.coordinatorlayout>
可以看到,这里只是将根布局从framelayout修改成了coordinatorlayout,其他都没有任何变化。然后重新运行程序。效果如下图所示:
这样就可以成功实现沉浸式状态栏效果了。
话说为什么android:fitssystemwindows属性,设置在coordinatorlayout布局上就能生效,设置在framelayout布局上就没有效果呢?
这是因为,xml中的配置毕竟只是一个标记,如果想要在应用程序当中产生具体的效果,那还是要看代码中是如何处理这些标记的。
很明显,framelayout对于android:fitssystemwindows属性是没有进行处理的,所以不管设不设置都不会产生什么变化。
而coordinatorlayout则不同,我们可以观察它的源码,如下所示:
private void setupforinsets() { if (build.version.sdk_int < 21) { return; } if (viewcompat.getfitssystemwindows(this)) { if (mapplywindowinsetslistener == null) { mapplywindowinsetslistener = new androidx.core.view.onapplywindowinsetslistener() { @override public windowinsetscompat onapplywindowinsets(view v, windowinsetscompat insets) { return setwindowinsets(insets); } }; } // first apply the insets listener viewcompat.setonapplywindowinsetslistener(this, mapplywindowinsetslistener); // now set the sys ui flags to enable us to lay out in the window insets setsystemuivisibility(view.system_ui_flag_layout_stable | view.system_ui_flag_layout_fullscreen); } else { viewcompat.setonapplywindowinsetslistener(this, null); } }
可以看到,这里当发现coordinatorlayout设置了android:fitssystemwindows属性时,会对当前布局的insets做一些处理,并且调用了下面一行代码:
setsystemuivisibility(view.system_ui_flag_layout_stable | view.system_ui_flag_layout_fullscreen);
这行代码是一切的关键所在。准确来讲,就是因为执行了这行代码,我们才能将布局的内容延伸到系统状态栏区域。
是不是感觉解密了?但事实上coordinatorlayout所做的事情还远不止这些。
因为沉浸式状态栏其实会带来很多问题。让布局的内容延伸到状态栏的背后,如果一些可交互的控件被状态栏遮挡了怎么办?这样这些控件可能就无法点击和交互了。
coordinatorlayout为了解决这个问题,会对所有内部的子view都进行一定程度的偏移,保证它们不会被状态栏遮挡住。
比如我们在coordinatorlayout当中再添加一个按钮:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff66ff" android:fitssystemwindows="true"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </androidx.coordinatorlayout.widget.coordinatorlayout>
运行一下程序,效果如下图所示:
可以看到,虽然coordinatorlayout延伸到了状态栏区域,但是它所包含的按钮是不会进入状态栏区域的,这样就避免了可交互控件被遮挡的情况出现。
但有的朋友会说,如果有些子控件我就是想要让它也延伸到状态栏区域内呢?比如我在coordinatorlayout内放了一张图片,按照这个规则,图片也是不会显示在状态栏背后的,这样就达不到想要的效果了。
我们可以来试一下这种场景。比如在coordinatorlayout中再添加一个imageview,代码如下所示:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff66ff" android:fitssystemwindows="true"> <imageview android:layout_width="match_parent" android:layout_height="match_parent" android:scaletype="centercrop" android:src="@drawable/bg" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </androidx.coordinatorlayout.widget.coordinatorlayout>
现在运行一下程序,效果如下图所示:
确实,图片是不会进入状态栏区域的,和我们之前所解释的理论相符合。
但是很明显,这并不是我们想要的效果,那么有什么办法可以解决呢?
这里我们可以借助其他布局来实现。在google提供的诸多布局当中,并不是只有coordinatorlayout会处理android:fitssystemwindows属性,像collapsingtoolbarlayout、drawerlayout也是会对这个属性做处理的。
现在对activity_main.xml进行如下修改:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff66ff" android:fitssystemwindows="true"> <com.google.android.material.appbar.collapsingtoolbarlayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true"> <imageview android:layout_width="match_parent" android:layout_height="match_parent" android:scaletype="centercrop" android:src="@drawable/bg" android:fitssystemwindows="true" /> </com.google.android.material.appbar.collapsingtoolbarlayout> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </androidx.coordinatorlayout.widget.coordinatorlayout>
可以看到,这里我们在imageview的外面又包裹了一层collapsingtoolbarlayout,并且给collapsingtoolbarlayout也设置了android:fitssystemwindows属性,这样collapsingtoolbarlayout就可以将内容延申到状态栏区域了。
接着我们给imageview同样设置了android:fitssystemwindows属性,如此一来,就可以让图片显示在状态栏的背后了。
重新运行一下程序,效果如下图所示:
需要注意的是,collapsingtoolbarlayout一定要结合着coordinatorlayout一起使用,而不能单独使用。因为collapsingtoolbarlayout只会对内部控件的偏移距离做出调整,而不会像coordinatorlayout那样调用setsystemuivisibility()函数来开启沉浸式状态栏。
看到这里,相信大家都已经知道应该如何去实现沉浸式状态栏效果了。但是可能有的朋友会说,由于项目限制的原因,他们无法使用coordinatorlayout或collapsingtoolbarlayout,而是只能使用像framelayout或linearlayout这样的传统布局,这种情况怎么办呢?
其实我们知道coordinatorlayout实现沉浸式状态栏的原理之后,自然也就知道如何自己手动实现了,因为本质就是调用setsystemuivisibility()函数。
现在我们将activity_main.xml改成用传统framelayout布局的写法:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff66ff"> <imageview android:layout_width="match_parent" android:layout_height="match_parent" android:scaletype="centercrop" android:src="@drawable/bg" /> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </framelayout>
为了实现沉浸式状态栏的效果,我们手动在mainactivity当中调用setsystemuivisibility()函数,来将framelayout的内容延伸到状态栏区域:
class mainactivity : appcompatactivity() { override fun oncreate(savedinstancestate: bundle?) { super.oncreate(savedinstancestate) setcontentview(r.layout.activity_main) window.statusbarcolor = color.transparent val framelayout = findviewbyid<framelayout>(r.id.root_layout) framelayout.systemuivisibility = (system_ui_flag_layout_stable or system_ui_flag_layout_fullscreen) } }
这里提醒一点,setsystemuivisibility()函数其实已经被废弃了。从android 11开始,google提供了一个新的api windowinsetscontroller来实现同样的功能,不过本篇文章就不往这方面展开了。
现在重新运行一下程序,效果如下图所示:
可以看到,现在我们仍然实现了沉浸式状态栏的效果,但问题是framelayout中的按钮也延伸到状态栏区域了,这就是前面所说的可交互控件被状态栏遮挡的问题。
出现这个问题的原因也很好理解,因为之前我们是使用的coordinatorlayout嘛,它已经帮我们考虑好到这些事情,自动会将内部的控件进行偏移。而现在framelayout显然是不会帮我们做这些事情的,所以我们得想办法自己解决。
这里其实可以借助setonapplywindowinsetslistener()函数去监听windowinsets发生变化的事件,当有监听到发生变化时,我们可以读取顶部insets的大小,然后对控件进行相应距离的偏移。
修改mainactivity中的代码,如下所示:
class mainactivity : appcompatactivity() { override fun oncreate(savedinstancestate: bundle?) { super.oncreate(savedinstancestate) setcontentview(r.layout.activity_main) window.statusbarcolor = color.transparent val framelayout = findviewbyid<framelayout>(r.id.root_layout) framelayout.systemuivisibility = (system_ui_flag_layout_stable or system_ui_flag_layout_fullscreen) val button = findviewbyid<button>(r.id.button) viewcompat.setonapplywindowinsetslistener(button) { view, insets -> val params = view.layoutparams as framelayout.layoutparams params.topmargin = insets.systemwindowinsettop insets } } }
可以看到,当监听到windowinsets发生变化时,我们调用systemwindowinsettop即可获取到状态栏的高度,然后对不需要延伸到状态栏区域的控件进行相应的偏移即可。
重新运行程序,效果如下图所示:
好了,到这里为止,我们就将实现沉浸式状态栏背后的原理,以及具体的多种实现方式都介绍完了。
这次你学懂android:fitssystemwindows属性了吗?
到此这篇关于android 超详细讲解fitssystemwindows属性的使用的文章就介绍到这了,更多相关android fitssystemwindows属性内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!