在Android中去掉标题栏有三种方法,我知道的。

在java代码里实现

  1. this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏

注:这句代码要写在setContentView()前面。

在清单文件(manifest.xml)里面实现

  1. <application android:icon="@drawable/icon"
  2. android:label="@string/app_name"
  3. android:theme="@android:style/Theme.NoTitleBar">

这样用可以将整个应用设置成无标题栏,如果只需要在一个Activity设置成一个无标题栏的形式,只要把上面的第三行代码写到某一个Activity里面就可以了。

在style.xml文件里定义

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <resources>
  3. <style name="notitle">
  4. <item name="android:windowNoTitle">true</item>
  5. </style>
  6. </resources>

然后面manifest.xml中引用就可以了,这种方法稍麻烦了些。

  1. <application android:icon="@drawable/icon"
  2. android:label="@string/app_name"
  3. android:theme="@style/notitle">

其实可以看得出来,第二种方法和第三种方法实质是一样的,只不过第二种方法调用的是系统定义好的style.xml文件,而第三种方法则是在自己的应用里定义style.xml,然后再自己再调用,其实道理是一样的,第三种方法做起来更有成就感。

style parent为Theme.AppCompat.Light时,直接修改parent:

  1. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  2. <!-- Customize your theme here. -->
  3. <item name="colorPrimary">@color/colorPrimary</item>
  4. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  5. <item name="colorAccent">@color/colorAccent</item>
  6. </style>