How to hide the title bar for an Activity in XML with existing custom theme

前端 未结 30 1677
误落风尘
误落风尘 2020-11-22 03:28

I want to hide the titlebar for some of my activities. The problem is that I applied a style to all my activities, therefore I can\'t simply set the theme to @android:

相关标签:
30条回答
  • In your onCreate method, use the following snippet:

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    
    0 讨论(0)
  • 2020-11-22 03:45

    You just need to change AppTheme style in Style.xml if you replace the definition from DarkActionBar

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

    to NoActionBar

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

    the AppTheme defined in AndroidManifast.xml

    0 讨论(0)
  • 2020-11-22 03:45

    In my case, if you are using android studio 2.1, and your compile SDK version is 6.0, then just go to your manifest.xml file, and change the following code:

    Here is the code:

    <?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>
    

    And here is the snip shoot(see the highlight code):

    0 讨论(0)
  • 2020-11-22 03:47

    when i tried to use all those high upvoted answers my app always crashed. (i think it has something to do with the "@android:style"?)

    The best solution for me was to use the following:

    android:theme="@style/Theme.AppCompat.NoActionBar"
    

    No header / title bar anymore. Just place it in the <application>...</application> or <activity>...</activity> depending if you (don't) want it in the whole app or just a specific activity.

    0 讨论(0)
  • 2020-11-22 03:50

    Add this style to your style.xml file

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

    After that reference this style name into your androidManifest.xml in perticular activity in which you don't want to see titlebar, as like below.

    <activity android:name=".youractivityname"
         android:theme="@style/AppTheme.NoActionBar">
    </activity>
    
    0 讨论(0)
  • 2020-11-22 03:50

    Or if you want to hide/show the title bar at any point:

    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);
    }
    
    0 讨论(0)
提交回复
热议问题