can not remove title bar in Activity

2020-08-20 11:14发布

问题:

i want to remove title on top of my Activity. i tried this two way they work but not in api level 8.

1:

<style name="NOtitleTheme" parent="Theme.AppCompat.Light">
<item name="android:windowFullscreen">true</item>
   <item name="android:windowNoTitle">true</item> </style>

2:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

any idea why this does not work on api 8?

once i managed to remove it in api 8, but i do not remember how.


EDIT:

Solved! the problem was this.requestWindowFeature(Window.FEATURE_NO_TITLE) does not work on API 8, and also <item name="android:windowNoTitle">true</item> does not work, but actionBar.hide() works , the reason that getSupportActionBar() returns null was using one of the two option before, i just forget to remove both. when i remove one i add other(what a a stupid !) thanks all! (sorry for bad English)

回答1:

Add to your Manifest:

for full screen:

        <activity
        android:name="youtpackagename.activityname"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

or (hide title alone)

       <activity
        android:name="youtpackagename.activityname"
        android:theme="@android:style/Theme.NoTitleBar" >

or else use below code before setContentView method .

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

or

{
  ActionBar actionBar = getActionBar(); or getSupportActionBar();
  actionBar.hide();
}

Edit:

final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);

Its working fine below api 8 also.. i hope this will help you



回答2:

    <resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->

         <item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
    </style>

</resources> 

add this in your styles xml to your AppTheme.

And

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

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

}

Add this to your activity. Use ActionBarActivity instead of Activity.



回答3:

To me the only way that worked was doing this:

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

Only by saying that my theme was a child of NoActionBar, I managed to eliminate the actionBar. All the other ways failed. Don't know why.



回答4:

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

use requestWindowFeature method before setContentView method .



回答5:

Your "workaround" (hiding the actionBar yourself) is the normal way. But google recommends to always hide the actionbar when the Title Bar is hidden. Have a look here: https://developer.android.com/training/system-ui/status.html

If your build version is less than 16 use it as given below

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // If the Android version is lower than Jellybean, use this call to hide
    // the status bar.
    if (Build.VERSION.SDK_INT < 16) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    setContentView(R.layout.activity_main);
}
...

}