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


当前回答

最好的方法是将第一个活动作为空白活动和您想要放置的内容,然后在一段时间后触发另一个活动。 按照这种方法,你可以将第一个活动作为你的启动画面,而不需要操作栏或任何东西。 这是我的第一个活动

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

}
}

之后,你可以把任何一个活动作为第一个活动 如果你想在任何屏幕上都没有动作栏,那么只需在你的样式中添加这个

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

其他回答

@牧师的解决方案工作。但这似乎也是谷歌的一些原生应用程序的问题:谷歌、play store、talk。还有其他大型应用程序,如skype。

编辑:下面的解决方案给了我的问题actionbarsherlock api < 4.0,原因是setTheme不工作前冰淇淋三明治

在应用程序或活动标签的清单中添加以下内容:

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

然后在你的主要活动中

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

    // Start regular onCreate()
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

在阅读了所有可用选项后,我发现最好的方法是设置主主题而不使用ActionBar,然后在所有活动的父级代码中设置MyTheme。

清单:

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

BaseActivity:

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

这种方法可以帮助我在应用程序启动时避免使用ActionBar !

嗨,我有一个简单的解决方案,使用2个主题

启动画面主题(添加到清单): <style name="SplashTheme" parent="@android:style/Theme.Holo.NoActionBar"> <项目名称= " android: windowBackground " > @color /红> < /项目 > < /风格 正常的主题(添加到你的活动setTheme(R.style.Theme)): <style name="Theme" parent="@style/Theme. "Holo"> <item name="android:windowBackground">@color/blue</item> . > < /风格

支持SDK 10:

@Override    
public void onCreate(Bundle savedInstanceState) {

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

      ...........
      ...........
}

创建两个样式:

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

将AppThemeNoBar设置为应用程序主题,将AppThemeBar设置为您想要显示actionbar的活动 使用两种样式,当视图加载时,你将看不到操作栏。

检查这个链接Android:隐藏操作栏,同时查看加载

设置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();