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

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

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


当前回答

隐藏标题和动作栏我这样做:

在Manifest的activity标签中:

android:theme="@style/AppTheme.NoActionBar"

在Activity.java setContentView之前:

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

也就是说,

//删除标题栏

this.requestWindowFeature (Window.FEATURE_NO_TITLE);

//删除通知栏

this.getWindow () .setFlags (WindowManager.LayoutParams。FLAG_FULLSCREEN WindowManager.LayoutParams.FLAG_FULLSCREEN);

注意:你需要在setContentView之前保留这些行

其他回答

你可以修改AndroidManifest.xml:

<activity android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

或者使用android:theme="@android:style/ theme . black。NoTitleBar"如果你不需要全屏活动。

注意:如果你之前使用了一个'default'视图,你可能也应该把父类从AppCompatActivity改为Activity。

如果你替换了DarkActionBar的定义,你只需要改变style .xml中的AppTheme样式

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

对NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

在androidmanifest .xml中定义的AppTheme

如果你做用户YaW和Doug Paul说的,那么你必须记住在调用setContentView之前必须设置窗口特性。如果不是,您将得到一个异常。

我不喜欢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。“全屏”也会有同样的效果,还会删除通知栏。

在onCreate方法中,使用以下代码段:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);