Android 菜单和子菜单

发布于 2024-12-02 04:14:22 字数 1751 浏览 4 评论 0原文

我在创建单选按钮子菜单时遇到问题,当选择用户按下菜单按钮时出现的选项菜单中的菜单项时,会显示该单选按钮子菜单。这是我到目前为止所得到的:

// Expand the options menu when the user taps their menu button
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    switch item.getItemId() {
        case R.id.expandRadioMenu:
            // Show the sub-menu and collapse the initial menu
            return true;
    }
}

res/menu/menu.xml 的 XML 如下:

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:visible="true" 
        android:enabled="true" 
        android:checkable="false" 
        android:icon="@drawable/ic_menu_mark" 
        android:id="@+id/expandRadioMenu" 
        android:title="Select" 
        android:titleCondensed="select">
        <menu>
        <group 
            android:enabled="true" 
            android:visible="false" 
            android:checkableBehavior="single" 
            android:id="@+id/radio">
            <item 
                android:enabled="true" 
                android:visible="true" 
                android:title="Foo" 
                android:titleCondensed="Foo" 
                android:id="@+id/foo">
            </item>
            <item 
                android:enabled="true" 
                android:visible="true" 
                android:title="Bar" 
                android:titleCondensed="Bar" 
                android:id="@+id/bar">
            </item>
        </group>
        </menu>
    </item>
</menu>

I'm having trouble creating a radio-button sub-menu that is shown when a menu item from the options menu that appears when a user presses the menu button is selected. This is what I have so far:

// Expand the options menu when the user taps their menu button
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    switch item.getItemId() {
        case R.id.expandRadioMenu:
            // Show the sub-menu and collapse the initial menu
            return true;
    }
}

The XML of res/menu/menu.xml is as follows:

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:visible="true" 
        android:enabled="true" 
        android:checkable="false" 
        android:icon="@drawable/ic_menu_mark" 
        android:id="@+id/expandRadioMenu" 
        android:title="Select" 
        android:titleCondensed="select">
        <menu>
        <group 
            android:enabled="true" 
            android:visible="false" 
            android:checkableBehavior="single" 
            android:id="@+id/radio">
            <item 
                android:enabled="true" 
                android:visible="true" 
                android:title="Foo" 
                android:titleCondensed="Foo" 
                android:id="@+id/foo">
            </item>
            <item 
                android:enabled="true" 
                android:visible="true" 
                android:title="Bar" 
                android:titleCondensed="Bar" 
                android:id="@+id/bar">
            </item>
        </group>
        </menu>
    </item>
</menu>

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

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

发布评论

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

评论(1

双手揣兜 2024-12-09 04:14:22

我知道它不是特别吸引人,但是您是否尝试过将子菜单移动到单独的 .xml 文件中?我以前见过它的工作原理,但这不是一个非常有希望的解决方案。我想它可能会进一步确定问题所在。

I know it's not particularly appealing, but have you tried moving the sub menu into a separate .xml file? I've seen it work before, but it's not a terribly hopeful solution. I suppose it might further identify the problem though.

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