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

前端 未结 30 1631
误落风尘
误落风尘 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条回答
  • 2020-11-22 03:38

    I believe there is just one line answer for this in 2020

    Add the following line to the styles.xml

    <item name="windowNoTitle">true</item>
    
    0 讨论(0)
  • 2020-11-22 03:39

    The title bar can be removed in two ways as mentioned on the developer Android page:

    In the manifest.xml file:

    1. Add the following in application if you want to remove it for all the activities in an app:

      <application android:theme="@android:style/Theme.Black.NoTitleBar">
      
    2. Or for a particular activity:

      <activity android:theme="@android:style/Theme.Black.NoTitleBar">
      
    0 讨论(0)
  • 2020-11-22 03:41

    If you use this.requestWindowFeature(Window.FEATURE_NO_TITLE) user will still be able to see the title bar just for a moment during launch animation when activity starts through onCreate. If you use @android:style/Theme.Black.NoTitleBar as shown below then title bar won't be shown during launch animation.

    <activity 
        android:name=".MainActivity" 
        android:label="My App"
        android:theme="@android:style/Theme.Black.NoTitleBar"
        android:screenOrientation="portrait">
    

    above example will obviously override your existing application theme, if you have existing theme then add <item name="android:windowNoTitle">true</item> to it.

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

    This is how the complete code looks like. Note the import of android.view.Window.

    package com.hoshan.tarik.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Window;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:43

    Do this in your onCreate() method.

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

    this refers to the Activity.

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

    add in manifiest file ,

      android:theme="@android:style/Theme.Translucent.NoTitleBar"
    

    add following line into ur java file,

      this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    
    0 讨论(0)
提交回复
热议问题