使用appcompat_v7,定义activity全屏或无标题栏

来源:互联网 发布:子弹路径算法 编辑:程序博客网 时间:2024/05/13 01:35

         我刚开始使用Google新推出的appcompat_v7的时候,发现当项目引用这个兼容项目并且Activity继承ActionBarActivity后,就必须使用Theme.Appcompat系列的Style才行,不然程序运行会报错的。

       换个主题好说,于是把程序的Style换了,程序运行就不报错了。但是问题又来了,我要Activity不显示标题栏,但是Theme.Appcompat系列的主题中就没有NoTitleBar之类的,那怎么设置主题呢?

       我试着自己在Styles.xml文件中自己写了一个MyAppTheme,如下:

[html] view plain copy
  1. <!--  
  2.     Base application theme, dependent on API level. This theme is replaced  
  3.     by AppBaseTheme from res/values-vXX/styles.xml on newer devices.  
  4. -->  
  5. <style name="AppBaseTheme" parent="Theme.AppCompat.Light">  
  6.     <!--  
  7.         Theme customizations available in newer API levels can go in  
  8.         res/values-vXX/styles.xml, while customizations related to  
  9.         backward-compatibility can go here.  
  10.     -->  
  11. </style>  
  12.   
  13. <!-- Application theme. -->  
  14. <style name="AppTheme" parent="AppBaseTheme">  
  15.     <!-- All customizations that are NOT specific to a particular API-level can go here. -->  
  16. </style>  
  17.   
  18. <!-- Activity无标题栏 开始-->  
  19. <style name="MyAppTheme" parent="AppTheme">  
  20.     <item name="android:windowNoTitle">true</item>  
  21. </style>  
  22. <!-- Activity无标题栏 结束-->  

注意AppBaseTheme我改成了Theme.AppCompat.Light,并且要把values-v11和values-v14文件夹中的styles.xml里的AppBaseTheme也一起修改掉)。

       修改后,在AndroidManifest.xml文件中,把application节点的Android:theme属性设置为“@style/MyAppTheme”。

       然后在我的手机上运行起来,OK,没有问题,确实标题栏没有了,达到了我想要的效果。

       不过还没完,我又把我的两个安卓虚拟机跑起来,系统版本分别是4.4.2和2.3.3,放到虚拟机上测试。4.4.2的虚拟机没问题,显示效果跟真机一样,但是2.3.3就没用了,还是有标题栏存在……奇怪

       后来又试了半天无果,跑到stackoverflow上面查了一下,终于有收获了。发现应该是2.x的系统下style还需要windowActionBar的属性设置为false才行,修改后的MyAppTheme如下:

[html] view plain copy
  1. <!-- Activity无标题栏 开始-->  
  2. <style name="MyAppTheme" parent="AppTheme">  
  3.     <item name="android:windowNoTitle">true</item>  
  4.     <item name="windowActionBar">false</item>  
  5. </style>  
  6. <!-- Activity无标题栏 结束-->  

       


再用2.3.3的虚拟机测试,OK,不会显示标题栏了,并且4.x的虚拟机上也没有问题。

注意要添加一个代码在自己xml布局的根节点中加入:

android:fitsSystemWindows="true" android:clipToPadding="true"
不然会出现:


自己的布局被通知栏给覆盖了。

-----------------------------------------------------------------------------------

       如果还需要全屏的效果,再给style中加上如下的两个属性就行了:


[html] view plain copy
  1. <item name="android:windowFullscreen">true</item>  
  2. <item name="android:windowContentOverlay">@null</item>                                                              
0 0
原创粉丝点击