安卓:点击后变更动作条图标的onCreate回来后改变它() [英] Android: Change actionbar icon after click and change it back after onCreate()

查看:196
本文介绍了安卓:点击后变更动作条图标的onCreate回来后改变它()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改 onOptionsItemSelected(菜单项)操作栏,以进度条的图标方法。

    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    case R.id.progressitem2:
        mProgress = item;
        mProgressCreate = mProgress;
        mProgress.setActionView(R.layout.progress);
        mLayout.removeView(mTable);
        // Execute code that change mTable again.
        return true;
    }

progress.xml文件:

progress.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:addStatesFromChildren="true"
              android:focusable="true"
              android:paddingLeft="4dp"
              android:paddingRight="4dp"
              android:gravity="center" >
    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            style="@android:style/Widget.ProgressBar.Small"/>

</LinearLayout> 

操作栏以这种方式创建的:

The action bar is created in this way:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actionbar, menu);
    mProgress = menu.getItem(0);
    mProgressCreate = mProgress;
    return true;
}

action_menu.xml

action_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/progressitem2" 
            android:icon="@drawable/ic_action_refresh"
            android:title="Reload"
            android:showAsAction="ifRoom|withText"
            android:visible="true" />
</menu>

这工作不错。进度图标显示后,我点击操作栏的图标。

This works good. The progress icon is shown after I click the action bar icon.

我尽量保持原有的操作栏符号的参考 mProgressCreate 并尝试添加动作视图回到了的onCreate年底()方法:

I try to keep the reference of the original action bar symbol in mProgressCreate and try to add the action view back at the end of the onCreate() method:

mProgress.setActionView(mProgressCreate.getActionView());

但是,这不工作...

But this do not work...

什么`错在这里?

问候, 桑德罗

推荐答案

我找到了解决办法。我只是忘了叫 invalidateOptionsMenu()之前,我与复位动作视图 mProgress.setActionView(mProgressCreate.getActionView());

I found the solution. I simply forgot to call invalidateOptionsMenu() before I reset the action view with mProgress.setActionView(mProgressCreate.getActionView());

invalidateOptionsMenu();
mProgress.setActionView(mProgressCreate.getActionView());

和一切工作正常: - )

And everything is working fine :-).

这篇关于安卓:点击后变更动作条图标的onCreate回来后改变它()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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