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

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

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


当前回答

只需使用getActionBar().hide();在你的主活动onCreate()方法中。

其他回答

在我的例子中,如果你使用的是android studio 2.1,并且你的编译SDK版本是6.0,那么只需转到你的manifest.xml文件,并更改以下代码:

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lesterxu.testapp2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

下面是截图(见高亮代码):

我更喜欢:-

AppTheme(整个应用程序主题) AppTheme。NoActionBar(没有操作栏或工具栏的主题) fullscreen(没有动作栏和状态栏的主题)

主题风格喜欢:-

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorDarkPrimary</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

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

<style name="AppTheme.NoActionBar.FullScreen" parent="AppTheme.NoActionBar">
    <item name="android:windowFullscreen">true</item>
</style>

在onCreate方法中也把下面的代码放在super.onCreate(savedInstanceState)之后

super.onCreate(savedInstanceState)    
this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)

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

将以下行添加到styles.xml中

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

现在我做了以下事情。

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

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

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

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

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

我使用@YaW的解决方案从我的活动中删除标题和通知。但是,标题和通知将在显示对话框时出现。因此,要将此应用到一个对话框,请将该对话框子类化,如下所示:

public class MyDialog extends android.app.Dialog{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.mydialog);
    }    
}