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

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

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


当前回答

对我有用的方法:

1-在styles.xml中:

 <style name="generalnotitle" parent="Theme.AppCompat.Light" >
          <item name="android:windowNoTitle">true</item>   <!-- //this -->     
   </style>

2-在MainActivity中

@Override
protected void onCreate(Bundle savedInstanceState) {          
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide(); //<< this
    setContentView(R.layout.activity_main);
}

在显式中继承样式: <活动android: name = "。MainActivity @style / generalnotitle“android:主题= >

其他回答

将此样式添加到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>

您可以在java文件中使用此代码

在设置或加载视图之前添加这一行

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

如果你使用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>到它。

在我的例子中,如果你使用的是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>

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

这解决了我的问题

在表现我的活动:

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

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

在“AppTheme”名称下的样式:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme"
        parent="Theme.AppCompat.Light.NoActionBar">
        **<item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>**
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>