开发者

android how to put sub-menu within submenu

I want to add a submenu to an existing submenu with this xml:

<item android:id="@+id/chooseCountry" android:title="Choose Country">
    <menu>
        <item android:id="@+id/india" android:title="India" >
            <menu>
                <item android:id="@+id/blor" android:title="Bangalore" />
                <item android:id="@+id/delhi" android:title="Delhi" />
                <item android:id="@+id/hyd" android:title="Hyderabad" />
            </menu>
        </item>
        <item android:id="@+id/pak" android:title="Pakistan" />
        <item android:id="@+id/africa" android:title="South Africa" />
    </menu>
</item>

I am getting but this exceptio开发者_运维问答n:

08-15 12:57:50.942: ERROR/AndroidRuntime(312): java.lang.UnsupportedOperationException: Attempt to add a sub-menu to a sub-menu.

I do not understand what I am doing wrong - can anybody give me some advice?


This appears to be natively supported from Android API level 15. On earlier versions, a workaround is to redirect menu calls. Using your xml example, change it so that the sub-sub-menu is a regular hidden menu item, and add a redirect place holder item instead:

<item android:id="@+id/india" android:title="India" android:visible="false">
    <menu>
        <item android:id="@+id/blor" android:title="Bangalore" />
        <item android:id="@+id/delhi" android:title="Delhi" />
        <item android:id="@+id/hyd" android:title="Hyderabad" />
    </menu>
</item>


<item android:id="@+id/chooseCountry" android:title="Choose Country">
<menu>
    <item android:id="@+id/india_redirect" android:title="India" />
    <item android:id="@+id/pak" android:title="Pakistan" />
    <item android:id="@+id/africa" android:title="South Africa" />
</menu>

Hang on to the inflated Menu mOptionsMenu created in onCreateOptionsMenu( Menu menu ) and open the sub-menu from your redirect like this:

@Override
public boolean onMenuItemSelected( int id, MenuItem item) 
{
    switch ( item.getItemId() ) 
    {
      case R.id.india_redirect:
        mSomeView.post( new Runnable()
        {
            public void run()
            {
                mOptionsMenu.performIdentifierAction( R.id.india, 0 );                  
            }
        } );

        return true;
    }

    return super.onMenuItemSelected( id, item );
}

Note the call to performIdentifierAction is posted from a View to the UI message queue. On old versions of Android it can be called immediately, but on newer versions it needs to be a separate message to work.


Well, apparently this is not supported. You'll have to redesign your menu.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜