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

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

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


当前回答

我相信,在2020年,这个问题只有一个答案

将以下行添加到styles.xml中

<item name="windowNoTitle">true</item>

其他回答

我发现了发生这种错误的两个原因。

一个。窗口标志已经在super.onCreate(savedInstanceState)中设置好了;在这种情况下,您可能需要使用以下命令的顺序:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

super.onCreate(savedInstanceState);

两个。在标题栏中有Back/Up按钮,这意味着当前活动是另一个活动的分层子活动,在这种情况下,您可能想要注释掉或从onCreate方法中删除这行代码。

getActionBar().setDisplayHomeAsUpEnabled(true);

添加主题@style/ theme . appcompat . light。在AndroidManifest.xml中的NoActionBar

<activity
        android:name=".activities.MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    </activity>

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

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);
}

在onCreate()方法中执行此操作。

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here); 

这是指活动。

咳咳,您可以将主题应用于XML中的单个活动,例如没有标题。换句话说,将其从应用程序标记中取出,打开标记声明并将其放入所需的活动标记中。