【问题标题】:Android why does setActionView hide menu itemAndroid为什么setActionView隐藏菜单项
【发布时间】:2014-10-26 20:09:28
【问题描述】:

当我尝试通过调用 MenuCompat.setActionView(menuitem, null) 从我的操作栏中删除操作视图时,我最终得到一个不可见且不可点击的菜单项。菜单项只是消失了,它只在我调用 setActionView 传递布局时显示。如果我调用 invalidateOptionsMenu(),它会显示出来,但会产生一些奇怪的结果,比如在需要时不显示进度条。

这是删除和添加 actionView 的函数。它是一个刷新动作:

public void refresh(boolean refreshing, boolean fragment) {
    if(refreshing == refreshed) {
        return;
    }
    refreshed = refreshing;

    final MenuItem refreshItem = optionsMenu.findItem(R.id.menu_refresh);

    if (optionsMenu != null) {
        if (refreshItem != null) {
            if (refreshing) {
                MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_indeterminate_progress);
                MenuItemCompat.expandActionView(refreshItem);
                if(fragment)
                    ((PhotoGridPagerFragment) fm.findFragmentByTag(TAG_FOR_PHOTO_GRID_PAGER_FRAGMENT)).refresh();
            } else {
                MenuItemCompat.setActionView(refreshItem, null);
            }
        }
    }
}

【问题讨论】:

  • 您是否有特殊原因想要在单个菜单项上设置/取消设置操作视图,而不是执行诸如拥有两个项目并将可见性设置为一个或另一个?
  • 菜单项也用作刷新按钮。当它点击它应该显示加载进度条。不知道你说的我是否还能做到这一点。 ?

标签: android android-layout android-activity android-actionbar


【解决方案1】:

MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_indeterminate_progress);

我认为您不能只发送布局 ID。您需要先对其进行充气,然后将其作为参数传递:

View newView = getLayoutInflater().inflate(R.layout.actionbar_indeterminate_progress);

MenuItemCompat.setActionView(refreshItem, newView);

但我们没有足够的信息来确定: optionsMenu 是在哪里定义的?

【讨论】:

  • 在 onCreateOptionsMenu 中
  • 我的问题是。 optionsMenu 是您的 Activity 类的成员 Var 吗?
  • 这个线程有点老了,我想试一试,但正如@ElAbogato 所说,我们需要更多信息。你能提供你的菜单xml文件吗?
【解决方案2】:

也许您忘记为 MenuItem 设置图标。

<item
    android:id="@+id/refreshMenuItem" android:title="@string/refresh"
    app:showAsAction="always" android:icon="@mipmap/refresh" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多