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

前端 未结 30 1678
误落风尘
误落风尘 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:55

    You can use this code in your java file

    add this line before you set or load your view

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

    what works for me:

    1- in styles.xml:

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

    2- in MainActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {          
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide(); //<< this
        setContentView(R.layout.activity_main);
    }
    
    1. in manifest inherit the style:

      <activity android:name=".MainActivity" android:theme="@style/generalnotitle">
      
    0 讨论(0)
  • 2020-11-22 03:57

    Add both of those for the theme you use:

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

    To Hide both Title and Action bar I did this:

    In activity tag of Manifest:

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

    In Activity.java before setContentView:

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

    i.e.,

    //Remove title bar

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //Remove notification bar

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

    NOTE: You need to keep these lines before setContentView

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

    Add theme @style/Theme.AppCompat.Light.NoActionBar in your activity on AndroidManifest.xml like this

    <activity
            android:name=".activities.MainActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        </activity>
    
    0 讨论(0)
  • 2020-11-22 03:57

    If you do what users YaW and Doug Paul say, then you have to have in mind that window features must be set prior to calling setContentView. If not, you will get an exception.

    0 讨论(0)
提交回复
热议问题