android2.3 api demo 学习系列(20)--App/Menu

现在来学习下menu的相关知识:

1、只有文字的菜单配置:

 

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

    <item android:id="@+id/jump"
        android:title="@string/app_menu_onlytitle_menu1" />

    <item android:id="@+id/dive"
        android:title="@string/app_menu_onlytitle_menu2" />
</menu>

 

 2、包含文字和图片的菜单配置

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/happy"
        android:title="Happy"
        android:icon="@drawable/menu_icon1" />
    <item android:id="@+id/neutral"
        android:title="Neutral"
        android:icon="@drawable/menu_icon2" />
    <item android:id="@+id/sad"
        android:title="Sad"
        android:icon="@drawable/menu_icon3" />
</menu>

 

 3、包含子菜单项

 

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

    <item android:title="Normal 1"/>
    <item
        android:id="@+id/submenu"
        android:title="Emotions">
        <menu>
            <item
                android:id="@+id/happy"
                android:icon="@drawable/menu_icon1"
                android:title="Happy"/>
            <item
                android:id="@+id/neutral"
                android:icon="@drawable/menu_icon2"
                android:title="Neutral"/>
            <item
                android:id="@+id/sad"
                android:icon="@drawable/menu_icon3"
                android:title="Sad"/>
        </menu>
    </item>
    <item android:title="Normal 2"/>

</menu>

 

 4、分组菜单项

 

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

    <item
        android:id="@+id/browser_visibility"
        android:title="@string/app_menu_title1"/>

    <group android:id="@+id/browser" >
        <item
            android:id="@+id/refresh"
            android:title="@string/app_menu_title2"/>
        <item
            android:id="@+id/bookmark"
            android:title="@string/app_menu_title3"/>
    </group>

    <item
        android:id="@+id/email_visibility"
        android:title="@string/app_menu_title4"/>

    <group android:id="@+id/email" >
        <item
            android:id="@+id/reply"
            android:title="@string/app_menu_title5"/>
        <item
            android:id="@+id/forward"
            android:title="@string/app_menu_title6"/>
    </group>

</menu>

 

 5、可单选、多选的菜单先

 

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

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

    <!-- Checkable items appear only in submenus or context menus. -->

    <!-- Carefully look at the attribute name checkableBehavior on groups, but
         the attribute name checkable on items. The checkableBehavior encompasses
         the number of items that will be checkable within that group. -->

    <item android:title="None">
        <menu>
            <!-- The none checkableBehavior is default, but we explicitly show it here. -->
            <group android:id="@+id/noncheckable_group"
                    android:checkableBehavior="none">
                <!-- Notice how these items inherit from the group. -->
                <item android:id="@+id/noncheckable_item_1"
                        android:title="@string/app_menu_title1" />
                <item android:id="@+id/noncheckable_item_2"
                        android:title="@string/app_menu_title2" />
                <item android:id="@+id/noncheckable_item_3"
                        android:title="@string/app_menu_title3" />
            </group>
        </menu>
    </item>

    <item android:title="All">
        <menu>
            <group android:id="@+id/checkable_group"
                    android:checkableBehavior="all">
                <!-- Notice how these items inherit from the group. -->
                <item android:id="@+id/checkable_item_1"
                        android:title="@string/app_menu_title1" />
                <item android:id="@+id/checkable_item_2"
                        android:title="@string/app_menu_title2"
                        android:checked="true" />
                <item android:id="@+id/checkable_item_3"
                        android:title="@string/app_menu_title3"
                        android:checked="true" />
            </group>
        </menu>
    </item>

    <item android:title="Single">
        <menu>
            <group android:id="@+id/exclusive_checkable_group"
                    android:checkableBehavior="single">
                <!-- Notice how these items inherit from the group. -->
                <item android:id="@+id/exclusive_checkable_item_1"
                        android:title="@string/app_menu_title1" />
                <item android:id="@+id/exclusive_checkable_item_2"
                        android:title="@string/app_menu_title2" />
                <item android:id="@+id/exclusive_checkable_item_3"
                        android:title="@string/app_menu_title3"
                        android:checked="true" />
            </group>
        </menu>
    </item>

    <item android:title="All without group">
        <menu>
            <!-- Notice how these items have each set. -->
            <item android:id="@+id/nongroup_checkable_item_1"
                    android:title="@string/app_menu_title1"
                    android:checkable="true" />
            <item android:id="@+id/nongroup_checkable_item_2"
                    android:title="@string/app_menu_title2"
                    android:checkable="true"
                    android:checked="true" />
            <item android:id="@+id/nongroup_checkable_item_3"
                    android:title="@string/app_menu_title3"
                    android:checkable="true"
                    android:checked="true" />
        </menu>
    </item>

</menu>

 

 6、菜单项排序

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/fourth_item"
        android:orderInCategory="3"
        android:title="Fourth" />

    <item android:id="@+id/third_item"
        android:orderInCategory="2"
        android:title="Third" />

    <item android:id="@+id/second_item"
        android:orderInCategory="1"
        android:title="Second" />

    <item android:id="@+id/first_item"
        android:orderInCategory="0"
        android:title="First" />
</menu>

 

 7、菜单项排序2,和上一个不通的是分组多了一个 android:menuCategory="secondary"排序

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/most_used_items">
    
        <item android:id="@+id/last_most_item"
            android:orderInCategory="10"
            android:title="@string/app_menu_title1" />
    
        <item android:id="@+id/middle_most_item"
            android:orderInCategory="7"
            android:title="@string/app_menu_title2"/>
            
        <item android:id="@+id/first_most_item"
            android:orderInCategory="4"
            android:title="@string/app_menu_title3"/>
    
    </group>
    
    <!-- This group uses the secondary category, which is used for less oftenly used items.
         Notice these items will show up after the above items.
         (Furthermore, notice how the orders in each category are independent from the other
         category.) -->
    <group android:id="@+id/least_used_items"
        android:menuCategory="secondary">
        
        <item android:id="@+id/last_least_item"
            android:orderInCategory="3"
            android:title="@string/app_menu_title4" />
    
        <item android:id="@+id/middle_least_item"
            android:orderInCategory="2"
            android:title="@string/app_menu_title5" />
    
        <item android:id="@+id/first_least_item"
            android:orderInCategory="0"
            android:title="@string/app_menu_title6" />
    
    </group>

</menu>

 

 8、菜单项的可见属性

 

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

    <item android:id="@+id/visible_item"
        android:title="Visible"
        android:alphabeticShortcut="a" />

    <item android:id="@+id/hidden_item"
        android:title="Hidden"
        android:visible="false"
        android:alphabeticShortcut="b" />

    <group android:id="@+id/hidden_group"
        android:visible="false">
    
        <item android:id="@+id/hidden_by_group"
            android:title="Hidden by group"
            android:alphabeticShortcut="c" />
    
    </group>

</menu>

 

 9、菜单项的enable属性

 

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

    <item android:id="@+id/enabled_item"
        android:title="Enabled"
        android:icon="@drawable/menu_icon1" />

    <item android:id="@+id/disabled_item"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/menu_icon2" />

    <item android:id="@+id/enabled_item_2"
        android:title="Enabled"
        android:icon="@drawable/menu_icon1" />

    <item android:id="@+id/disabled_item_2"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/menu_icon2" />

    <item android:id="@+id/enabled_item_3"
        android:title="Enabled"
        android:icon="@drawable/menu_icon3" />

    <item android:id="@+id/disabled_item_3"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/menu_icon4" />

    <item android:id="@+id/enabled_item_4"
        android:title="Enabled"
        android:icon="@drawable/menu_icon5" />

    <item android:id="@+id/disabled_item_4"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/menu_icon6" />

</menu>

 

 10、菜单项的快捷键设置

 

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

    <item
        android:id="@+id/invisible_item"
        android:alphabeticShortcut="i"
        android:title="Invisible item"
        android:visible="false"/>
    <item
        android:id="@+id/a_item"
        android:alphabeticShortcut="a"
        android:title="Alvin"/>
    <item
        android:id="@+id/b_item"
        android:alphabeticShortcut="b"
        android:title="Bart"/>
    <item
        android:id="@+id/c_item"
        android:alphabeticShortcut="c"
        android:title="Chris"/>
    <item
        android:id="@+id/d_item"
        android:alphabeticShortcut="d"
        android:title="David"/>
    <item
        android:id="@+id/e_item"
        android:alphabeticShortcut="e"
        android:title="Eric"/>
    <item
        android:id="@+id/f_item"
        android:alphabeticShortcut="f"
        android:title="Frank"/>
    <item
        android:id="@+id/g_item"
        android:alphabeticShortcut="g"
        android:title="Gary"/>
    <item
        android:id="@+id/h_item"
        android:alphabeticShortcut="h"
        android:title="Henry"/>
    <item
        android:id="@+id/excl_item"
        android:alphabeticShortcut="!"
        android:title="Exclamation"/>

</menu>

 

 菜单项的配置基本上都在xml完成,在代码加载后显示即可

 

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Hold on to this
        mMenu = menu;
        // Inflate the currently selected menu XML resource.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(menuRes, menu);
        return true;
    }

 

 菜单项的实现基本上很简单,另外还有一种菜单Context Menu,请参考sdk,后面到相关demo中再学习。

over。

你可能感兴趣的:(android,api,demo)