我想为我的一些活动隐藏标题栏。问题是,我应用了一个风格,我所有的活动,因此我不能简单地设置主题@android:style/ theme . notitlebar。

使用NoTitleBar主题作为我的样式的父主题将从我的所有活动中删除标题栏。

我可以在某个地方设置无标题样式的项目吗?


当前回答

或者如果你想隐藏/显示标题栏在任何点:

private void toggleFullscreen(boolean fullscreen)
{
    WindowManager.LayoutParams attrs = getWindow().getAttributes();
    if (fullscreen)
    {
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }
    else
    {
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }
    getWindow().setAttributes(attrs);
}

其他回答

如果你使用this.requestWindowFeature(Window.FEATURE_NO_TITLE),当活动通过onCreate启动时,用户仍然可以在启动动画期间看到标题栏。如果你使用@android:style/Theme.Black。NoTitleBar如下所示,标题栏将不会在启动动画期间显示。

<activity 
    android:name=".MainActivity" 
    android:label="My App"
    android:theme="@android:style/Theme.Black.NoTitleBar"
    android:screenOrientation="portrait">

上面的例子显然会覆盖你现有的应用主题,如果你有现有的主题,那么添加<item name="android:windowNoTitle">true</item>到它。

将此样式添加到style.xml文件中

 <style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
 </style>

之后,在你不想看到标题栏的特定活动中引用此样式名称到androidManifest.xml,如下所示。

<activity android:name=".youractivityname"
     android:theme="@style/AppTheme.NoActionBar">
</activity>

我不喜欢this.requestWindowFeature(Window.FEATURE_NO_TITLE);因为标题栏会短暂出现,然后消失。

我也不喜欢android:theme="@android:style/ theme。NoTitleBar”,因为我失去了新设备用户已经习惯的3.0+ Holo的所有变化。所以我想到了这个解。

在res/values文件夹中创建一个名为styles.xml的文件(如果它不存在的话)。在该文件中放置以下代码:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.Default" parent="@android:style/Theme"></style>
    <style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>
    <style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>
</resources>

接下来用另一个styles.xml文件创建res/values-v11(同样,这个文件可能已经存在)。在该文件中放置以下代码:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.Default" parent="@android:style/Theme.Holo"></style>
    <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.NoActionBar"></style>
    <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen"></style>
</resources>

如果你的目标是4.0以上,创建一个res/values-v14文件夹和另一个styles.xml文件(是的,它可能已经在那里了)。在该文件中放置以下代码:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.Default" parent="@android:style/Theme.Holo.Light"></style>
    <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
    <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></style>
</resources>

最后,创建好所有这些文件后,打开androidmanifest .xml文件,添加代码:

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

如果想要应用到整个应用程序,则添加到不需要标题的活动的活动标记或应用程序标记。

现在您的用户将获得与他们的设备版本相关联的主题和您想要的屏幕布局。

附注:将值更改为android:theme="@style/ theme。“全屏”也会有同样的效果,还会删除通知栏。

为你使用的主题添加这两个:

    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>

现在我做了以下事情。

我声明了一个样式,继承了我的通用样式的所有内容,然后禁用了标题栏。

<style name="generalnotitle" parent="general">
    <item name="android:windowNoTitle">true</item>
</style>

现在我可以将此样式设置为我想要隐藏标题栏的每个活动,覆盖应用程序范围的样式并继承所有其他样式信息,因此在样式代码中没有重复。

要将样式应用到特定的Activity,打开AndroidManifest.xml并将以下属性添加到Activity标签中;

<activity
    android:theme="@style/generalnotitle">