How to disable action bar permanently

2019-01-01 08:48发布

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?

25条回答
明月照影归
2楼-- · 2019-01-01 09:07

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 {
查看更多
不流泪的眼
3楼-- · 2019-01-01 09:07

in the onCreate function add the following code

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

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

查看更多
与君花间醉酒
4楼-- · 2019-01-01 09:07

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.

查看更多
高级女魔头
5楼-- · 2019-01-01 09:08

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 :)

查看更多
皆成旧梦
6楼-- · 2019-01-01 09:09

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.

查看更多
泛滥B
7楼-- · 2019-01-01 09:10

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" ... />
查看更多
登录 后发表回答