Android开发——去掉系统自带标题栏的几种方式

https://blog.csdn.net/qq_28585471/article/details/75991613

今天在练习自定义标题栏(Android初级开发(四)——补充3)的过程中遇到了隐藏系统自带标题栏的问题,现将几种去掉系统自带标题栏的方式做一总结。大体上可以分为两种方式,一种是修改xml文件(这种方式产生的效果作用于所有Activity),一种是编码实现(这种方式产生的效果只作用于当前Activity):

    方法1-1:

    1、查看清单文件AndroidManifest.xml中的theme

    android:theme="@style/AppTheme"(系统默认的) 保持不变

    2、在style.xml文件中修改AppTheme

    

 

 

    方法1-2    

    在清单文件AndroidManifest.xml中修改theme,使用系统自带的无标题样式

    实现无标题栏(但有系统自带的任务栏)

    android:theme = "@android:style/Theme.NoTitleBar

    实现全屏效果:

    android:theme = "@android:style/Theme.NoTitleBar.Fullscreen"

!!!这时,可能会有朋友发现自己运行后出现错误,提示You need to use a Theme.AppCompat theme (or descendant) with this activity.这是因为Activity继承自了android.support.v7.app.AppCompatActivity,而不是android.app.Activity。具体的解决方法有两种:

    1)如果不是强烈要求我们的Activity必须继承自AppCompatActivity,就直接让它继承Activity.如图

    

    2)如果还是想继承自AppCompatActivity,那么根据提示来使用AppCompat的theme,即将AndroidManifest.xml文件中关于Activity的theme配置改为:

    android:theme="@style/Theme.AppCompat.Light.NoActionBar"

    好了,运行程序,你会发现问题已经解决啦~!!

          

 

 

    方式1-3

    在清单文件AndroidManifest.xml中修改theme,使用自定义的无标题样式

    android:theme = "@style/NoTitle"

    在res/values/styles.xml文件中,加入如下代码

    <style name="NoTitle">
         <item name="android:windowNoTitle">true</item> </style>

 

 

    方法2

    在程序中编写代码进行设置,只需在onCreate()方法中加入如下代码:

    实现无标题栏(但有系统自带的任务栏)

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    实现全屏效果

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

           WindowManager.LayoutParams.FLAG_FULLSCREEN);

 

 

    我在参考其他小伙伴的隐藏标题栏相关资料时,发现很多人都喜欢在文章最后附上这个Android系统自带样式罗列表,本来我是不打算仿照他们的这个做法的,但是想着还是自己敲一遍加深印象,抱着这个目的,下面请见Android系统自带样式:)

附:Android系统自带样式

android:theme = "@android:style/Theme.Dialog" 将一个Activity显示为对话框模式

android:theme = "@android:style/Theme.NoTitleBar" 不显示应用程序标题栏

android:theme = "@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,并全屏

android:theme = "Theme.Light" 背景为白色

andorid:theme = "Theme.Light.NoTitleBar" 白色背景并无标题栏

android:theme = "Theme.Black" 背景为黑色

android:theme = "Theme.Black.NoTitleBar" 黑色背景并无标题栏

android:theme = "Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏

android:theme = "Theme.Wallpaper" 用系统桌面为应用程序背景

android:theme = "Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏

android:theme = "Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏

android:theme = "Translucent" 透明背景

android:theme = "Theme.Translucent.NoTitleBar" 透明背景并无标题

android:theme = "Theme.Translucent.NoTitleBar.Fullscreen" 透明背景并无标题,全屏

android:theme = "Theme.Panel" 面板风格显示

android:theme = "Theme.Light.Panel" 平板风格显示

 

   

猜你喜欢

转载自www.cnblogs.com/LuoEast/p/9443778.html