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条回答
泛滥B
2楼-- · 2019-01-01 12:36

The solutions already posted came with the sideffect, that the first .show() call did not animate the ActionBar for me. I got another nice solution, which fixed that:

Create a transparent drawable - something like that:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
       android:color="#00000000" />
</shape>

Set the actual actionbar background to a invisible custom view which you set on the actionbar:

getSupportActionBar().setCustomView(R.layout.actionbar_custom_layout);
      getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
              ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);

Set the transparent background for the actionbar in onCreate:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_transparent));

Imortant: Don't hide the actionbar immediately in onCreate, but with a little delay later - e.g. when the layout is finished with creation:

getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    getSupportActionBar().hide();
                }
            });

Before your first .show() call set the custom view visible:

_actionbarRoot.setVisibility(View.VISIBLE);
getSupportActionBar().show();
查看更多
高级女魔头
3楼-- · 2019-01-01 12:40

Android studio provide in build template for full screen, if you use Android studio you can follow below step to implement full screen activity.

First Step to Create new Activity

Select select Full screen Activity

Done. Android studio did your job, now you can check code for full screen.

查看更多
倾城一夜雪
4楼-- · 2019-01-01 12:42

The best way to do this is two make the first activity as blank activity and the content you want to put and then after some time fire another activity. By following this way you can make the first activity as your splash screen without action bar or anything. Heres my first activity

package com.superoriginal.rootashadnasim.syllabus;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class first_screen extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_screen);


    final Handler h=new Handler();
    h.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent=new     Intent(getApplicationContext(),DrawerChooseBranchMainActivity.class);
            startActivity(intent);
        }
    },2000);

}
}

After that you can put any of your activity as first activity If you want no action bar in any screen then just add this in your styles

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
查看更多
不流泪的眼
5楼-- · 2019-01-01 12:43

Create two styles:

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

<style name="AppThemeBar" parent="Theme.AppCompat.Light">
    <item name="android:windowNoTitle">false</item>
</style>

Set AppThemeNoBar as application theme and AppThemeBar to the activity where you want to show the ActionBar. Using two styles you wont see the Action bar while views are loading.

Check this link Android: hide action bar while view load

查看更多
听够珍惜
6楼-- · 2019-01-01 12:44

Hi I have a simple solution by using 2 themes

  1. Splash screen theme (add it to the manifest):

    <style name="SplashTheme" parent="@android:style/Theme.Holo.NoActionBar"> <item name="android:windowBackground">@color/red</item> </style>

  2. normal theme (add it in your activity by setTheme(R.style.Theme)):

    <style name="Theme" parent="@style/Theme.Holo"> <item name="android:windowBackground">@color/blue</item> </style>

To support SDK 10:

@Override    
public void onCreate(Bundle savedInstanceState) {

    setTheme(R.style.Theme);      
    super.onCreate(savedInstanceState);

      ...........
      ...........
}
查看更多
浅入江南
7楼-- · 2019-01-01 12:44

2015, using support v7 library with AppCompat theme, set this theme for your Activity.

<style name="AppTheme.AppStyled" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimaryDark">@color/md_indigo_100</item>
    <item name="colorPrimary">@color/md_indigo_500</item>
    <item name="colorAccent">@color/md_red_500</item>
    <item name="android:textColorPrimary">@color/md_white_1000</item>
    <item name="android:textColor">@color/md_purple_500</item>
    <item name="android:textStyle">bold</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>
查看更多
登录 后发表回答