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

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

问题描述

我要实现启动画面在我的蜂窝应用程序。 我用这个code。在活动的onCreate来显示启动:

I need to implements splash screen in my honeycomb app. I use this code in activity's onCreate to show splash:

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

这code,显示主界面了一段时间后:

and this code to show main UI after some time:

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.

我怎样才能使它不显示?

How can I made it not to show?

我试图申请主题活动,无须操作栏:

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.

推荐答案

设置安卓windowActionBar =假真正禁用动作条,但随后,正如你所说的, getActionBar(); 返回null。 这是通过解决:

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

此创建动作条,并立即隐藏它收到了将被显示的机会。

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

但是现在有一个问题。把后 windowActionBar =假的主题,活动吸引了动作条的正常窗口标题来代替。
如果我们试图通过使用一些 * NoTitleBar 股票主题,以避免这种情况,或者我们尽量把&LT;项目名称=机器人:windowNoTitle。 &GT;真&LT; /项目&GT; 在我们自己的主题,它不会工作
。 其原因在于,动作条的依赖的的窗口标题,以显示自己 - 这是动作条是转变窗口标题
因此,关键它可以帮助我们摆脱是增加一件事对我们活动的主题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();

和你做。该活动将启动,没有动作条闪烁,也不窗口标题显示。

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

ADDON: 如果你显示和隐藏的动作条多次也许你已经注意到了第一的说明是的没有的动画。从那以后的显示和隐藏的动画。如果你想有动画的第一次展出过,你可以使用这个:

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

同样的事情可以实现:

The same thing can be achieved with:

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

但它可能需要一些额外的逻辑来判断这是否是活动的第一次展出。

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

我们的想法是推迟了一点动作条的隐藏。在某种程度上,我们让动作条显示,但随后立即将其隐藏。因此,我们超越了第一个非动画显示和明年的表现将被视为第二个,从而将动画。

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.

正如你可能已经猜到了是有机会的动作条的可以的可以看到之前就已经被隐藏了延期手术。这是真正的情况。大多数时候没有看到,但还没有,过一段时间,你可以看到动作条闪烁的一瞬间。

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.

在任何情况下,这不是一个pretty的解决办法,所以我欢迎任何建议。

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

添加作为V7支持动作条的用户,在code将是:

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

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

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

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