▶ Inflate from XML

실행화면

초기 화면                                                               콤보 박스 펼침 화면

 Title only 일때 메뉴                                                 Titils and Icon 일때 메뉴

 Submenu 일때 메뉴                                               Submenu Emotitions 선택시 메뉴

  Group 일때 메뉴                                                Brower visibility 선택시 메뉴(Refresh,Bookmark 유/무)

Email visibility 선택시 메뉴(Reply,Forward 유/무)        Checkable 일때 메뉴                        

    Shortcuts 일때 메뉴                                           Shortcut의 More일때 메뉴                                    

    Order 일때 메뉴                                                 Visible 일때 메뉴                                             

        Disabled 일때 메뉴                                            Category and Order 일때 메뉴

소스 위치 : src/com/example/android/apis/app/MenuInflateFromXml.java

각 메뉴 종류와 사용법

▦ 
MenuInflateFromXml Activity
메뉴의 종류별xml을 지정된 배열 변수에 저장
    private static final int sMenuExampleResources[] = {
        R.menu.title_only, R.menu.title_icon, R.menu.submenu, R.menu.groups,
        R.menu.checkable, R.menu.shortcuts, R.menu.order, R.menu.category_order,
        R.menu.visible, R.menu.disabled

title_only.xml 파일 내용
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/jump"
        android:title="@string/jump" />
    <item android:id="@+id/dive"
        android:title="@string/dive" />
</menu>

title_icon.xml 파일 내용
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/happy"
        android:title="Happy"
        android:icon="@drawable/stat_happy" />
    <item android:id="@+id/neutral"
        android:title="Neutral"
        android:icon="@drawable/stat_neutral" />
    <item android:id="@+id/sad"
        android:title="Sad"
        android:icon="@drawable/stat_sad" />
</menu>

▦ submenu.xml 파일 내용
    <item android:title="Normal 1" />
    <item android:id="@+id/submenu"
        android:title="Emotions">
        <menu>        
            <item android:id="@+id/happy"
                android:title="Happy"
                android:icon="@drawable/stat_happy" />        
            <item android:id="@+id/neutral"
                android:title="Neutral"
                android:icon="@drawable/stat_neutral" />        
            <item android:id="@+id/sad"
                android:title="Sad"
                android:icon="@drawable/stat_sad" />        
        </menu>    
    </item>
    <item android:title="Normal 2" />

▦ groups.xml 파일 내용
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/browser_visibility"
        android:title="@string/browser_visibility" />
    <group android:id="@+id/browser">   
        <item android:id="@+id/refresh"
            android:title="@string/browser_refresh" />    
        <item android:id="@+id/bookmark"
            android:title="@string/browser_bookmark" />    
    </group>
    <item android:id="@+id/email_visibility"
        android:title="@string/email_visibility" />
    <group android:id="@+id/email">   
        <item android:id="@+id/reply"
            android:title="@string/email_reply" />    
        <item android:id="@+id/forward"
            android:title="@string/email_forward" />
    </group>
</menu>

▦ checkable.xml 파일 내용
<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/item_1" />
                <item android:id="@+id/noncheckable_item_2"
                        android:title="@string/item_2" />
                <item android:id="@+id/noncheckable_item_3"
                        android:title="@string/item_3" />
            </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/item_1" />
                <item android:id="@+id/checkable_item_2"
                        android:title="@string/item_2"
                        android:checked="true" />
                <item android:id="@+id/checkable_item_3"
                        android:title="@string/item_3"
                        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/item_1" />
                <item android:id="@+id/exclusive_checkable_item_2"
                        android:title="@string/item_2" />
                <item android:id="@+id/exclusive_checkable_item_3"
                        android:title="@string/item_3"
                        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/item_1"
                    android:checkable="true" />
            <item android:id="@+id/nongroup_checkable_item_2"
                    android:title="@string/item_2"
                    android:checkable="true"
                    android:checked="true" />
            <item android:id="@+id/nongroup_checkable_item_3"
                    android:title="@string/item_3"
                    android:checkable="true"
                    android:checked="true" />
        </menu>
    </item>
</menu>

▦ shortcuts.xml 파일 내용
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/invisible_item"
        android:visible="false"
        android:alphabeticShortcut="i"
        android:title="Invisible item" />
    <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>

▦ order.xml 파일 내용
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- These are in reverse order in this resource, but the orderInCategory attribute will
         order them for the menu (they all have the same default category). -->
    <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>

▦ category_order.xml 파일 내용
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This group uses the default category. -->
    <group android:id="@+id/most_used_items">    
        <item android:id="@+id/last_most_item"
            android:orderInCategory="10"
            android:title="@string/last_most_often" />    
        <item android:id="@+id/middle_most_item"
            android:orderInCategory="7"
            android:title="@string/middle_most_often" />    
        <item android:id="@+id/first_most_item"
            android:orderInCategory="4"
            android:title="@string/first_most_often" />    
    </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/last_least_often" />    
        <item android:id="@+id/middle_least_item"
            android:orderInCategory="2"
            android:title="@string/middle_least_often" />    
        <item android:id="@+id/first_least_item"
            android:orderInCategory="0"
            android:title="@string/first_least_often" />    
    </group>
</menu>

▦ visible.xml 파일 내용
<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>

▦ disabled.xml 파일 내용
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/enabled_item"
        android:title="Enabled"
        android:icon="@drawable/stat_happy" />
    <item android:id="@+id/disabled_item"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/stat_sad" />
    <item android:id="@+id/enabled_item_2"
        android:title="Enabled"
        android:icon="@drawable/stat_happy" />
    <item android:id="@+id/disabled_item_2"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/stat_sad" />
    <item android:id="@+id/enabled_item_3"
        android:title="Enabled"
        android:icon="@drawable/stat_happy" />
    <item android:id="@+id/disabled_item_3"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/stat_sad" />
    <item android:id="@+id/enabled_item_4"
        android:title="Enabled"
        android:icon="@drawable/stat_happy" />
    <item android:id="@+id/disabled_item_4"
        android:title="Disabled"
        android:enabled="false"
        android:icon="@drawable/stat_sad" />
</menu>

수직 layout 생성 (MenuInflateFromXml.java)
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

Spinner을 생성하여 사용자가 메뉴 xml을 선택 할 수 있도록 한다.
뷰를 생성할때 ID를 설정하고 인스턴스 상태를 자동으로 저장할것이다.
(layout.simple_spinner_item과 layout.simple_spinner_dropdown_item은 안드로이드 기본 제공 layout으로 보인다)
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, sMenuExampleNames);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        mSpinner = new Spinner(this);
        mSpinner.setId(R.id.spinner);
        mSpinner.setAdapter(adapter);

기본 LinearLayout에 spinner 추가
        layout.addView(mSpinner,
                new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));

TextView 생성 하고 설정하기
        mInstructionsText = new TextView(this);
        mInstructionsText.setText(getResources().getString(
                R.string.menu_from_xml_instructions_press_menu));

기본 LinearLayout에  TextView 추가
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(10, 10, 10, 10);
        layout.addView(mInstructionsText, lp);

centent view에 LinearLayout을 설정
        setContentView(layout);

메뉴 생성을 위한 onCreateOptionsMenu()
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenu = menu;
        
        // 현재 선택된 메뉴 xml 리소스를 확장한다
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu);
        
        // 이미 메뉴가 생성 되었다면 다른 menu xml을 더이상 선택 할수 없도록 spinner 를 사용하지 못하게 한다.
        mSpinner.setEnabled(false);
       
        // 지시하는 text를 변경한다.
        mInstructionsText.setText(getResources().getString(
                R.string.menu_from_xml_instructions_go_back));
       
        return true;
    }

boolean onCreateOptionsMenu(Menu menu)
: 맨 처음 메뉴가 열릴때 안드로이드 시스템에서 호출되는 Callback 함수
단 한번만 호출되며, 매번 화면에 보일때 메뉴를 업데이트 하려면 onPrepareOptionsMenu(Menu)를 보라.

메뉴에서 선택했을때의 동작을 위한 onOpensItemSelected()
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.jump:
                Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.dive:
                Toast.makeText(this, "Dive into the water!", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.browser_visibility:
                final boolean shouldShowBrowser = !mMenu.findItem(R.id.refresh).isVisible();
                mMenu.setGroupVisible(R.id.browser, shouldShowBrowser);
                break;
               
            case R.id.email_visibility:
                final boolean shouldShowEmail = !mMenu.findItem(R.id.reply).isVisible();
                mMenu.setGroupVisible(R.id.email, shouldShowEmail);
                break;
                
            default:
                // 하위 메뉴가 없을때 토스트 text를 띄워준다.
                if (!item.hasSubMenu()) {
                    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                break;
        }
       
        return false;
    }

boolean onOptionsItemSelected(MenuItem item)
: 메뉴에서 아이템을 선택했을때 호출된다.


신고

'Android > ApiDemos' 카테고리의 다른 글

App - Notification - Notifying Service Controller  (0) 2010.08.30
App - Notification - IncomingMessage  (0) 2010.04.30
App - Menu - Inflate from XML  (0) 2010.04.20
App - LauncherShortcuts  (0) 2010.04.20
App - Intents  (0) 2010.04.20
App - Dialog  (0) 2010.04.20
Posted by 까칠코더.


티스토리 툴바