如何在创建活动之前隐藏操作栏,然后再次显示它? [英] How to hide action bar before activity is created, and then show it again?

查看:31
本文介绍了如何在创建活动之前隐藏操作栏,然后再次显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的蜂窝应用程序中实现启动画面.我在活动的 onCreate 中使用此代码来显示飞溅:

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

但是 onCreate 被调用并出现飞溅之前,显示操作栏的时间很少.

But before onCreate is called and splash appears, there is small amount of time when action bar shown.

如何让操作栏不可见?

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

I tried to apply theme to activity without action bar:

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

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

but in that case getActionBar() always returns null and I found no way to show it again.

推荐答案

设置 android:windowActionBar="false" 确实禁用了 ActionBar 但是,正如你所说的,getActionBar(); 返回空值.这是通过以下方式解决的:

Setting android:windowActionBar="false" truly disables the ActionBar but then, as you say, getActionBar(); returns null. This is solved by:

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

这将创建 ActionBar 并在它有机会显示之前立即将其隐藏.

This creates the ActionBar and immediately hides it before it had the chance to be displayed.

但现在还有一个问题.将 windowActionBar="false" 放入主题后,Activity 绘制其正常的窗口标题而不是 ActionBar.
如果我们试图通过使用一些 *.NoTitleBar 股票主题来避免这种情况,或者我们尝试将 <item name="android:windowNoTitle">true</item> 在我们自己的主题中,它不起作用.
原因是 ActionBar 依赖于 Window Title 来显示自己 - 即 ActionBar 是转换后的窗口标题.
因此,可以帮助我们的技巧是在我们的活动主题 xml 中再添加一件事:

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>

这将使窗口标题的高度为零,因此几乎不可见.

This will make the Window Title with zero height, thus practically invisible .

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

In your case, after you are done with displaying the splash screen you can simply call

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

你就完成了.Activity 启动时不会出现 ActionBar 闪烁,也不会显示窗口标题.

and you are done. The Activity will start with no ActionBar flickering, nor Window Title showing.

附加:如果您多次显示和隐藏 ActionBar,您可能已经注意到第一次显示没有动画.从那时起,显示和隐藏都是动画的.如果你也想在第一次展示时有动画,你可以使用这个:

ADDON: If you show and hide the ActionBar multiple times maybe you have noticed that the first showing is not animated. From then on showing and hiding are animated. If you want to have animation on the first showing too you can use this:

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 的第一次显示.

but it may need some extra logic to check if this is the first showing of the Activity.

这个想法是稍微延迟ActionBar的隐藏.在某种程度上,我们让 ActionBar 显示出来,但随后立即将其隐藏.因此,我们超越了第一个非动画展示,下一个展示将被视为第二个展示,因此它将是动画的.

The idea is to delay a little the hiding of the ActionBar. In a way we let the ActionBar be shown, but then hide it immediately. Thus we go beyond the first non-animated showing and next showing will be considered second, thus it will be animated.

正如您可能已经猜到的那样,ActionBar 可以在被延迟操作隐藏之前被看到.实际情况确实如此.大多数时候什么都看不到,但偶尔,您可以看到 ActionBar 闪烁一瞬间.

As you may have guessed there is a chance that the ActionBar could be seen before it has been hidden by the delayed operation. This is actually the case. Most of the time nothing is seen but yet, once in a while, you can see the ActionBar flicker for a split second.

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

In any case this is not a pretty solution, so I welcome any suggestions.

添加 v7 支持操作栏用户,代码为:

Addition for v7 support actionbar user, the code will be:

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

这篇关于如何在创建活动之前隐藏操作栏,然后再次显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆