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

best and simple

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

查看更多
无色无味的生活
3楼-- · 2019-01-01 12:24

Try this, it works for me:

Below gets rid of activity's title bar

 requestWindowFeature(Window.FEATURE_NO_TITLE);

Below eliminates the notification bar to make the activity go full-screen

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

(Full Example Below) Take note: These methods were called before we set the content view of our activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Sets Application to full screen by removing action bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);    
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main); 

        // without this check, we would create a new fragment at each orientation change!
        if (null == savedInstanceState)
            createFragment();
    }
查看更多
人气声优
4楼-- · 2019-01-01 12:25

Best result to me was to create an activity with ThemeNoTitleBar and without content as launcher. Then this activity call directly to the other.

Of course if you want you can add content to the Splash Activity but in my case I just wanted to show application directly.

Manifest:

<activity
        android:name="com.package.SplashActivity"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Activity:

public class SplashActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //start StartActivity
    Intent intent = new Intent(this, StartActivity.class);
    startActivity(intent);
    finish();
}

}

查看更多
柔情千种
5楼-- · 2019-01-01 12:26

Using this simple code in your .class file to hide action bar

getSupportActionBar().hide();
查看更多
骚的不知所云
6楼-- · 2019-01-01 12:26

@Clerics solution works. But this appears to also be an issue with some of googles native apps: googles, play store, talk. Also other big apps like skype.

EDIT: Bellow solution have given me problem for actionbarsherlock on api < 4.0, the reason being setTheme doesn't work pre ice cream sandwich

Add following in your manifest within you application or activity tags:

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

And then in your main activity:

    // Set theme
    setTheme(R.style.YOUR_THEME);
    getSupportActionBar().setTitle(R.string.title);

    // Start regular onCreate()
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
查看更多
初与友歌
7楼-- · 2019-01-01 12:29

For Splashscreen you should use this line in manifest and don't use getActionBar()

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

and once when Splash Activity is finished in the main Activity use below or nothing

<item name="android:windowActionBar">true</item>
查看更多
登录 后发表回答