我需要在我的蜂窝应用程序中实现启动画面。 我在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,我发现没有办法再次显示它。


当前回答

Android studio提供了全面屏的构建模板,如果你使用Android studio,你可以按照下面的步骤来实现全面屏的活动。

完成了。Android工作室做了你的工作,现在你可以检查代码全屏。

其他回答

如果你正在使用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>

只需将此添加到MainActivity的onCreate函数中。

val actionBar = supportActionBar?.apply { 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(),告诉();

设置android:windowActionBar="false"真正禁用动作栏,但然后,正如你所说,getActionBar();返回null。 解决方法是:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();

    setContentView(R.layout.splash); // be sure you call this AFTER requestFeature

这将创建动作栏,并在它有机会显示之前立即隐藏它。

But now there is another problem. After putting windowActionBar="false" in the theme, the Activity draws its normal Window Title instead of an ActionBar. If we try to avoid this by using some of the *.NoTitleBar stock themes or we try to put <item name="android:windowNoTitle">true</item> in our own theme, it won't work. The reason is that the ActionBar depends on the Window Title to display itself - that is the ActionBar is a transformed Window Title. So the trick which can help us out is to add one more thing to our Activity theme xml:

<item name="android:windowActionBar">false</item>
<item name="android:windowTitleSize">0dp</item>

这将使窗口标题的高度为零,因此实际上是不可见的。

在您的情况下,在显示启动画面后,您可以简单地调用

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

这样就做完了。活动将开始没有动作栏闪烁,也没有窗口标题显示。

插件: 如果你多次显示和隐藏动作栏,你可能会注意到第一次显示的不是动画。从那时起,显示和隐藏是动画。如果你想在第一次显示时也有动画,你可以使用这个:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);

    // delaying the hiding of the ActionBar
    Handler h = new Handler();
    h.post(new Runnable() {     
        @Override
        public void run() {
            getActionBar().hide();
        }
    });

同样的事情也可以实现:

protected void onPostResume() {
    super.onPostResume();
    getActionBar().hide();

但是它可能需要一些额外的逻辑来检查这是否是Activity的第一次显示。

这个想法是延迟一点动作栏的隐藏。在某种程度上,我们让动作栏显示出来,但随后立即隐藏它。因此,我们超越了第一次非动画的展示,下一次展示将被认为是第二次,因此它将是动画的。

正如你可能已经猜到的那样,在动作栏被延迟操作隐藏之前,它有可能被看到。事实就是这样。大多数时候什么也看不见,但偶尔,你可以看到动作栏闪烁一秒钟。

无论如何,这都不是一个很好的解决方案,所以我欢迎任何建议。

添加为v7支持动作栏用户,代码将是:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().hide();

对我来说,最好的结果是用ThemeNoTitleBar创建一个活动,没有内容作为启动器。然后这个活动直接调用给对方。

当然,如果你愿意,你可以向Splash Activity添加内容,但在我的情况下,我只想直接显示应用程序。

清单:

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

活动:

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();
}

}