我需要在我的蜂窝应用程序中实现启动画面。 我在activity的onCreate中使用这段代码来显示splash:

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

这段代码将显示主UI:

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

但在onCreate被调用和飞溅出现之前,有一小段时间,当动作栏显示。

我怎样才能使动作栏不可见?

我尝试将主题应用于没有动作条的活动:

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

但在这种情况下,getActionBar()总是返回null,我发现没有办法再次显示它。


当前回答

如果你正在使用ActionBarSherlock,那么在你的活动中使用Theme. sherlock . noactionbar Theme

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

其他回答

如果你正在使用ActionBarSherlock,那么在你的活动中使用Theme. sherlock . noactionbar Theme

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

对于Splashscreen,你应该在manifest中使用这一行,而不是使用getActionBar()

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

当Splash活动在主活动中完成时,使用下面或什么都没有

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

2015年,使用支持v7库与AppCompat主题,设置此主题为您的活动。

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

这是我用过的最好的方法 进入java文件,在onCreate之后:

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

        // Take instance of Action Bar 
        // using getSupportActionBar and 
        // if it is not Null 
        // then call hide function 
        if (getSupportActionBar() != null) { 
            getSupportActionBar().hide(); 
        } 
    }

解决方案已经发布了附带的副作用,第一个.show()调用没有为我动画动作栏。 我得到了另一个很好的解决方案,解决了这个问题:

创建一个透明的可绘制对象——就像这样:

<形状xmlns: android = " http://schemas.android.com/apk/res/android " > <固体 android:颜色= " # 00000000 " / > < / >形状

将实际的动作栏背景设置为你在动作栏上设置的一个不可见的自定义视图:

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

在onCreate中为动作栏设置透明背景:

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

重要提示:不要立即在onCreate中隐藏动作栏,但稍后会有一点延迟-例如,当布局创建完成时:

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

在你第一次调用.show()之前,设置自定义视图可见:

_actionbarRoot.setVisibility (View.VISIBLE); getSupportActionBar(),告诉();