从兼容性 Fragment 添加/更改兼容性 SimpleMenu/MenuItem

发布于 2025-01-01 20:29:01 字数 608 浏览 3 评论 0原文

我意识到这是一个特定的问题,但我觉得其他处理兼容性的人一定已经处理过这个问题。

当用户在 ViewPager 中的各个片段中滑动时,我还希望 actionBar 菜单项发生变化。我不确定这对于 兼容性操作栏是否可以轻松实现,任何指示或帮助将不胜感激。我只是通过 setTitle() 更改标题,因为 ActionBarHelper 处理了这个问题,但是我找不到任何用于更新菜单项的内容。我尝试了以下操作,但失败了。

public void setMenuDynamically(int resId){
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(resId, menu);
}

查看代码,似乎应该有一种简单/明显的方法来获取 SimpleMenu 的句柄并添加项目并设置其图标。

提前致谢(我希望谷歌男孩们能按照 Android 开发者 Google+ 的建议阅读这篇文章)

I realise this is a specific problem however I feel like others who are dealing with compatibility must have dealt with this.

As the user swipes through various fragments in the ViewPager I would also like the actionBar menu items to change. I am not sure if this is easily doable for the compatibility actionbar, any direction or help would be extremely appreciated. I am changing the title simply via setTitle() since the ActionBarHelper handles this however i cant find anything for updating the menu items. I tried the following but it fails..

public void setMenuDynamically(int resId){
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(resId, menu);
}

Looking through the code it seems there should be an easy/obvious way to get a handle to the SimpleMenu and add an item and set its icon.

Thanks in advance ( I am hoping the google boys are reading this as the android developers Google+ suggests)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2025-01-08 20:29:01

好吧,如果我描述的事情很糟糕,或者只是对这个主题没有太多了解,请告诉我。如果是第一个我真的很抱歉。不管怎样,我似乎让它发挥作用,但不确定这是否是正确的方法。

AcitonBarHelper

    public void updateMenu(MenuItem item) {
}

ActionBarHelperBase (适用于 2.2 - 3.0 设备)

@Override
public void updateMenu(MenuItem item){
    addActionItemCompatFromMenuItem(item);
}

并为 honeycomb 和 ICS 创建类似的方法,

最后我有一个用于页面更改的侦听器,并且在该侦听器中我调用...

public void setMenuDynamically(int resId, String title){
    MenuItem item = menu.add(title);
    item.setIcon(resId);
    getActionBarHelper().updateMenu(item);
    MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}

我不确定 MenuItemCompat 是否是必要的,但我仍然包含了它。至少对于 2.2 来说一切似乎都工作得很好。我很可能必须在覆盖中进行更改,但我可以从这里处理自己。

Ok well please let me know if I suck as describing things or if there just is not much knowledge on this topic. If the first I am really sorry guys. Regardless I seemed to get this to work but am unsure if this is the correct way.

AcitonBarHelper

    public void updateMenu(MenuItem item) {
}

ActionBarHelperBase (for 2.2 - 3.0 devices )

@Override
public void updateMenu(MenuItem item){
    addActionItemCompatFromMenuItem(item);
}

And create similar methods for honeycomb and ICS

finally i have a listener for page changing and in that listener i call...

public void setMenuDynamically(int resId, String title){
    MenuItem item = menu.add(title);
    item.setIcon(resId);
    getActionBarHelper().updateMenu(item);
    MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}

I am not sure if the MenuItemCompat is necessary but I included it nonetheless. Everything seems to work great for 2.2 at least. I will most likely have to make changes in the Overrides but I can handle myself from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文