Android ActionBar ActionProvider子菜单

时间:2013-11-19 17:23:48

标签: android android-actionbar

我无法让ActionProvider显示一个子菜单,我不明白为什么。我的菜单在xml中定义:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
        android:id="@+id/dossier_menu"
        android:showAsAction="always"
        android:actionProviderClass="com.some.other.mockup.MActionProvider">
</item>
</menu>

动作提供者类:

public class MActionProvider extends ActionProvider  {

private static final String TAG = "MActionProvider";
private static final int LIST_LENGTH = 3;
private Context context;

/**
 * Creates a new instance.
 *
 * @param context Context for accessing resources.
 */
public MezzActionProvider(Context context) {
    super(context);
    this.context = context;
}

@Override
public View onCreateActionView() {
    View view = View.inflate(context, R.layout.action_layout, null);
    //view must be returned not tetxView or just buttonView
    return view;
}

@Override
public boolean hasSubMenu() {
    Log.d(TAG, "hasSubMenu");
    return true;
}


@Override
public boolean onPerformDefaultAction() {
    Log.d(TAG, "onPerformDefaultAction");
    return super.onPerformDefaultAction();
}

/**add submenu in code**/
@Override
public void onPrepareSubMenu(SubMenu subMenu) {
    Log.d(TAG, "onPrepareSubMenu");

    subMenu.clear();
    subMenu.add(Menu.NONE, Menu.NONE, 1,"Mezz 1");
    subMenu.add(Menu.NONE, Menu.NONE, 2, "Mezz 2");

}

}

和行动布局是:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mezz State ++"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="4dp"/>
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="@dimen/buttonHeight"
        android:src="@android:drawable/ic_menu_add"
        android:layout_alignParentLeft="true"
        android:id="@+id/img1"
        android:layout_below="@id/textView"/>

我尝试使用按钮而不是图像视图,甚至尝试将子菜单添加到xml中的菜单项,当我点击动作提供程序时它只是没有显示任何子菜单我无法弄清楚原因。

由于

1 个答案:

答案 0 :(得分:2)

我在这里找到了答案:

Can't display sub-menu for custom ActionProvider

问题是我的onCreateActionView()方法不返回null。

感谢Jason Robinson的回答是:

@Override
public View onCreateActionView() {
  View view = View.inflate(context, R.layout.action_layout, null);

  final PopupMenu menu = new PopupMenu(mContext, view);
  menu.inflate(R.menu.submenu);
  menu.setOnMenuItemClickListener(this);

  view.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v){
        menu.show();
    }
  });

return view;
}

和submenu.xml包含:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:id="@+id/mezzSub1"
            android:title="Mezz 1"
            android:showAsAction="always"></item>
    <item
            android:id="@+id/mezzSub2"
            android:title="Mezz 2"
            android:showAsAction="always"></item>

</menu>