更改视图菜单项中的操作栏 [英] Changing view on Menu Item in Action Bar

查看:126
本文介绍了更改视图菜单项中的操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现我的应用程序中的刷新按钮允许用户手动重新同步到网络服务器。在code工作,但我无法搞清楚的操作意见(至少,我认为这就是我应该使用)。

I'm attempting to implement a refresh button in my app to allow a user to manually re-sync to a web server. The code works, but I'm having trouble figuring out the action views (at least, I think that's what I'm supposed to be using).

我的菜单项是在这里:

<item
    android:id="@+id/main_menu_refresh"
    android:enabled="true"
    android:icon="@drawable/refresh"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="@string/refresh" 
    android:actionViewClass="android.widget.ProgressBar">
</item>

现在的问题是,它总是显示进度条。我想知道,如果它像搜索小工具(唯一的例子,我真正看到在线),并添加 collapseActionView 标签 showAsAction 和$ P $从马上显示出来pvented它。然而,当我点击刷新按钮,图标会消失(好),但这样做在操作栏上的标题,以及进度条出现在那里的称号曾经是窗口的左侧。也不是我想要的。

The problem is, it always shows the ProgressBar. I wondered if it worked like the search widget (the only example I really see online) and added the collapseActionView tag to the showAsAction and that prevented it from showing up immediately. However, when I click the refresh button, the icon disappears (good), but so does the Title in the action bar, and the ProgressBar appears on the left side of the window where the title used to be. Also not what I wanted.

由于一些最后的努力,我试图把它添加到我的code,并删除 actionViewClass 从XML:

As something of a last ditch effort, I attempted to add this to my code, and remove the actionViewClass from the XML:

MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
Log.w("MyApp", "Have Menu");
ProgressBar pb = new ProgressBar(ReadingList.this);
refresh.setActionView(pb);

这是没有任何工作,对 setActionView 给我一个空指针错误。

That didn't work either, giving me a null pointer error on setActionView.

我需要,我可以从任何函数调用的解决方案后(有一个自动同步周期一开始我想在进度条到时也显示),并能够将其返回到它的静态图标。

I need a solution that I can call from any function (there is an auto-sync period at the beginning I would like the ProgressBar to display during as well), and be able to return it to it's static icon after.

我试图通过<一读href="http://stackoverflow.com/questions/8198253/progressbar-in-an-actionbar-like-gmail-app-with-refresh">this问题,但我无法理解什么答案的意思。我觉得我试图做的只是它说什么,但我想不会。任何援助多少AP preciated。

I tried reading through this question, but I am having trouble understanding what the answer means. I feel like I was trying to do just what it says, but I guess not. Any assistance is much appreciated.

编辑:对于sastraxi的建议

For sastraxi's suggestion.

public class IconSwitcher extends LinearLayout{



public IconSwitcher(Context context) {
    super(context);

    ProgressBar pb = new ProgressBar(context);
    ImageView iv = new ImageView(context);

    addView(iv);
    addView(pb);
}
}

这是我的课迄今。然而,当我尝试和参考吧:

This is my class thus far. However, when I try and reference it with:

MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
IconSwitcher ic = (IconSwitcher) refresh.getActionView();

我得到一个空指针错误。在创建IconSwitcher。按钮XML是如下:

I get a null pointer error. on creating the IconSwitcher. The button XML is as follows:

<item
    android:id="@+id/main_menu_refresh"
    android:enabled="true"
    android:icon="@drawable/refresh"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="@string/refresh" 
    android:actionViewClass="IconSwitcher">
</item>

我只是随便在引用该IconSwitcher查看一个艰难的时刻。

I'm just having a difficult time on referencing that IconSwitcher View.

编辑2:我appearantly有麻烦引用菜单项,在所有

Edit 2: I'm appearantly having trouble referencing the Menu Item at all.

MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
refresh.setVisible(false);

也给了我一个空指针,当我尝试设置的可见性。什么是错我参考?

Also gives me a null pointer when I try and set the visibility. What is wrong with my references?

推荐答案

我想它了。我发现这个previous <一href="http://stackoverflow.com/questions/7142722/replacing-an-actionbar-menu-item-icon-with-an-indeterminate-progressbar">SO题。从那里,我发现code谷歌写了这样<一个href="http://developer.android.com/resources/samples/ActionBarCompat/src/com/example/android/actionbarcompat/ActionBarHelperHoneycomb.html"相对=nofollow>此处。它基本上是复制和粘贴在这一点上,和完美的作品。

I figured it out. I found this previous SO question. From there, I found code Google wrote on doing this here. It was basically copy and paste at that point, and works perfectly.

这篇关于更改视图菜单项中的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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