How to hide action bar before activity is created,

2019-01-01 12:18发布

I need to implement splash screen in my honeycomb app. I use this code in activity's onCreate to show splash:

setContentView(R.layout.splash);
getActionBar().hide();

and this code to show main UI after sometime:

setContentView(R.layout.main);
getActionBar().show();

But before onCreate is called and splash appears, there is small amount of time when action bar shown.

How can I make action bar invisible?

I tried to apply theme to activity without action bar:

<item name="android:windowActionBar">false</item>

but in that case getActionBar() always returns null and I found no way to show it again.

25条回答
浮光初槿花落
2楼-- · 2019-01-01 12:31

If you use one Activity included a splash screen, then you can do this if you use SherlockActionBar

getSupportActionBar().hide();

After the splash screen you can show it again with ...

getSupportActionBar().show();

It should be the same with native ActionBar of Android.

查看更多
梦醉为红颜
3楼-- · 2019-01-01 12:31

I realise that posting links are not the best way to do things, but I highly recommend you read the following documentation from Google themselves. This is the official android doc on how to control your system ui (things like actionbar, nav bar etc). Unfortunately the info is too much to post directly, but after reading this you will understand exactly how to show and hide features no matter what version you are developing for, its so simple!

Incase the link ever changes, it can be found under the official android documentation under training -> getting started -> Best practices for user interface -> managing the system ui

https://developer.android.com/training/system-ui/index.html

enter image description here

查看更多
只若初见
4楼-- · 2019-01-01 12:32

In case you have null because you are using the support library, instead of getActionBar() you need to call getSupportActionBar().

查看更多
余生请多指教
5楼-- · 2019-01-01 12:33

I was also trying to hide Action Bar on Android 2.2, but none of these solution worked. Everything ends with a crash. I checked the DDMS LOg, It was telling me to use 'Theme.AppCompat'.At last I Solved the problem by changing the android:theme="@android:style/Theme.Holo.Light.NoActionBar"Line

into android:theme="@style/Theme.AppCompat.NoActionBar"and it worked, but the the Interface was dark.

then i tried android:theme="@style/Theme.AppCompat.Light.NoActionBar" and finally got what i wanted.

After that when I Searched about 'AppCompat' on Developer Site I got This.

So I think the Solution for Android 2.2 is

<activity
    android:name=".MainActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

And Sorry for my bad English Like Always.

查看更多
不再属于我。
6楼-- · 2019-01-01 12:33

The best way I find after reading all the available options is set main theme without ActionBar and then set up MyTheme in code in parent of all Activity.

Manifest:

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

BaseActivity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.GreenHoloTheme);
}

This way helps me to avoid ActionBar when application start!

查看更多
无与为乐者.
7楼-- · 2019-01-01 12:36

If you are using ActionBarSherlock, then use Theme.Sherlock.NoActionBar Theme in your Activity

<activity 
    android:name=".SplashScreenActivity"
    android:theme="@style/Theme.Sherlock.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
查看更多
登录 后发表回答