How to disable action bar permanently

2019-01-01 08:53发布

问题:

I can hide the action bar in honeycomb using this code:

getActionBar().hide();

But when the keyboard opens, and user copy-pastes anything, the action bar shows again. How can I disable the action bar permanently?

回答1:

If you are using Theme.Holo.Light and want to use the Theme.Holo.Light.NoActionBar variant on pre 3.2 devices you can add this to your styles.xml:

<style name=\"NoActionBar\" parent=\"@android:style/Theme.Holo.Light\">
    <item name=\"android:windowActionBar\">false</item>
    <item name=\"android:windowNoTitle\">true</item>
</style> 

and then set it as your activity\'s theme:

<activity android:theme=\"@style/NoActionBar\" ... />


回答2:

By setting activity theme in Manifest,

<activity
android:theme=\"@android:style/Theme.NoTitleBar.Fullscreen\"
....
>


回答3:

I use the following code inside my onCreate function:

ActionBar actionBar = getSupportActionBar();
actionBar.hide();

Source: https://developer.android.com/guide/topics/ui/actionbar.html



回答4:

<application
    .
    .
    android:theme=\"@android:style/Theme.Holo.Light.NoActionBar\"
    . >

maybe this help you



回答5:

With the Android Studio default generated Activity superclass is ActionBarActivity and then, none of solution in other responses works. To solve just change superclass:

public class xxxActivity extends ActionBarActivity{

to:

public class xxxActivity extends Activity {


回答6:

The best way I found which gives custom themes and no action bar, is to create a SuperClass for all activities in my project and in it\'s onCreate() call the following line of code -

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

It always work for me. The only issue in this approach is, you\'ll see action bar for a fraction of second when starting the app (Not the activity, the complete app).



回答7:

If you want to get full screen without actionBar and Title.

Add it in style.xml

<style name=\"AppTheme.NoActionBar\" parent=\"Theme.AppCompat.Light.DarkActionBar\">
      <item name=\"windowActionBar\">false</item>
      <item name=\"windowNoTitle\">true</item>
      <item name=\"android:windowFullscreen\">true</item>
</style>

and use the style at manifest.xml.

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


回答8:

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


回答9:

Go to styles.xml Change this DarkActionBar to NoActionBar

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


回答10:

Don\'t use Holo theme and the Actionbar will disappear. This code is working for me on API 8+, with support lib v7:

<style name=\"NoActionBar\" parent=\"@style/Theme.AppCompat.Light\">
<item name=\"android:windowNoTitle\">true</item>
</style>

set that theme for your activity:

<activity android:theme=\"@style/NoActionBar\" ... />

and in your activity class.

It works even when it extends ActionBarActivity.



回答11:

Heres a quick solution.

You find styles.xml and you change the base application theme to \"Theme.AppCompat.NoActionBar\" as shown below.

<style name=\"AppTheme\" parent=\"Theme.AppCompat.NoActionBar\">
    <!-- Customize your theme here. -->
</style>


回答12:

If you just want a theme with no action bar you can use \'NoActionBar\' variant, for eg. if your base theme is as below

<!-- Base application theme. -->
<style name=\"AppTheme\" parent=\"Theme.AppCompat.Light.DarkActionBar\">
    <item name=\"colorPrimary\">@color/colorPrimary</item>
    <item name=\"colorPrimaryDark\">@color/colorPrimaryDark</item>
    <item name=\"colorAccent\">@color/colorAccent</item>
</style>

then you can use

<style name=\"AppThemeNoActionBar\" parent=\"Theme.AppCompat.Light.NoActionBar\">
    <item name=\"colorPrimary\">@color/colorPrimary</item>
    <item name=\"colorPrimaryDark\">@color/colorPrimaryDark</item>
    <item name=\"colorAccent\">@color/colorAccent</item>
</style>

But if you want to retain the properties of your main theme i.e. AppTheme you can do as below

<style name=\"AppThemeNoActionBar\" parent=\"AppTheme\">
    <item name=\"windowActionBar\">false</item>
    <item name=\"windowNoTitle\">true</item>
</style>

You can retain all the properties of your base theme this way and don\'t have to explicitly add them in your NoActionBar theme :)



回答13:

Type this code to your onCreate method before setContentView:

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


回答14:

You can force hide the action bar simply:

ActionBar  actionbar = getSupportActionBar();
actionBar.hide();

Just use your default theme.



回答15:

Make sure you create a new theme by changing the name of the default theme in the styles.xml file to:

 <style name=\"MyTheme\" parent=\"android:Theme.Material.Light.NoActionBar\">

and than change the theme from the drop-down under your main_activity.xml to whatever you called your theme.



回答16:

Below are the steps for hiding the action bar permanently:

  1. Open app/res/values/styles.xml.
  2. Look for the style element that is named \"apptheme\". Should look similar to <style name=\"AppTheme\" parent=\"Theme.AppCompat.Light.DarkActionBar\">.
  3. Now replace the parent with any other theme that contains \"NoActionBar\" in its name.

    a. You can also check how a theme looks by switching to the design tab of activity_main.xml and then trying out each theme provided in the theme drop-down list of the UI.

  4. If your MainActivity extends AppCompatActivity, make sure you use an AppCompat theme.



回答17:

Another interesting solution where you want to retain the ViewPager while removing the action bar is to have a style as show

<style name=\"AppTheme\" parent=\"AppBaseTheme\">
    <item name=\"android:windowActionBarOverlay\">true</item>
    <item name=\"android:actionBarStyle\">@style/NoActionBarStyle</item>
    <item name=\"android:windowContentOverlay\">@null</item>
</style>
<style name=\"NoActionBarStyle\" parent=\"android:Widget.Holo.ActionBar\">
    <item name=\"android:backgroundSplit\">@null</item>
    <item name=\"android:displayOptions\"></item>
</style>

This is the way most of the Dialer applications in android is showing the ViewPager without the action bar.

Ref: https://github.com/CyanogenMod/android_packages_apps_Dialer



回答18:

in the onCreate function add the following code

ActionBar actionBar = getSupportActionBar();
actionBar.hide();

and just import android.support.v7.app.ActionBar



回答19:

try this in your manifist

 <activity
        android:name=\".MainActivity\"
        android:theme=\"@android:style/Theme.Holo.NoActionBar\"
        android:label=\"@string/app_name\" >


回答20:

  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.hide();
    }
    setContentView(R.layout.activity_main);


回答21:

There are two ways to disable ActionBar in Android.

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


回答22:

I would like to post rather a Designer approach to this, this will keep design separate from your business logic:

Step 1. Create new style in (res->values->styles.xml) : Basically it is copy of your overall scheme with different parent - parent=\"Theme.AppCompat.Light.NoActionBar\"

<!-- custom application theme. -->
    <style name=\"MarkitTheme\" parent=\"Theme.AppCompat.Light.NoActionBar\">
        <!-- Customize your theme here. -->
        <item name=\"colorPrimary\">@color/colorPrimary</item>
        <item name=\"colorPrimaryDark\">@color/colorPrimary</item>
        <item name=\"colorAccent\">@color/colorAccent</item>

        <item name=\"android:windowNoTitle\">true</item>
    </style>

Step 2: In your AndroidManifest.xml, add this theme to the activity you want in: e.g. I want my main activity without action-bar so add this like below:

<activity android:name=\".MainActivity\"
            android:theme=\"@style/MarkitTheme\">

This is the best solution for me after trying a lot of things.



回答23:

Under res -> values ->strings.xml

Change

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

To

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



回答24:

I use this solution:

in the manifest, inside your activity tag:

android:label=\"@string/empty_string\"

and in strings.xml:

<string name=\"empty_string\">\"\"</string>

This way you keep ActionBar (or Toolbar) with the title, but when Activity if created the title is automatically empty.



回答25:

Strangely enough, none of these worked for me when building on a Nexus 7 running 4.4.2 (API 19). For the most part, at least with the latest version of Android Studio (1.2.1.1) after creating a blank activity app, the:

android:theme=\"@style/AppTheme\"

is in the application element, not the activity element. If I removed the above code or tried to change it to:

android:theme=\"@android:style/Theme.Holo.Light.NoActionBar\"

the app won\'t run...if this is happening to you, just go into your styles.xml file (res > values > styles.xml) and add the .NoTitleBar to the end of the parent like this block:

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

No changes to the AndroidManifest file are needed.