Android 資源詳解(二) 菜單資源

來源:互聯網
上載者:User

標籤:android   資源   菜單   

    菜單資源也是一個XML檔案,定義於res/menu/main.xml目錄,用R.menu.main引用


    典型的菜單資源檔結構是:<menu>根原素,在其中嵌套<item>和<group>子項目,<item>中也可以嵌套<menu>元素。

    <menu>根項目中沒有屬性,它包含<item>和<group>元素

    <group>表示一個菜單組,相同的屬性可以在菜單組中設定

    group常見屬性如下:

         id: 唯一標示該菜單組的引用 id 。

         menuCategory: 對菜單進行分類,定義菜單的優先順序,有效值為 container 、system 、             secondary 和 altemative 。

         orderInCategory: 一個分類排序整數。

         checkableBehavior: 選擇行為,單選、多選還是其他。有效值為 none 、all 和 single 。

         visible: 是否可見, true 或者 false 。

         enabled: 是否可用, true 或者 false 。

    <item>表示功能表項目,包含在<menu>或<group>   中的有效屬性。 

    <item>元素的屬性說明如下:

         id: 唯一標示菜單的 ID 引用。

         menuCategory: 菜單分類。

         or由rlnCategory :分類排序。

         title: 功能表標題字串。

          titleCondensed:  濃縮標題,適合標題太長的時候使用。

         icon: 菜單的表徵圖。

         alphabeticShortcut: 字元快速鍵。

         numericShortcut: 數字快速鍵。

         checkable: 是否可選。

         checked: 是否已經被選。

         visible: 是否可見。

         enabled: 是否可用。


                        菜單資源的使用

    在menu目錄中開啟相應的.xml檔案,添加以下代碼

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.example.new1.MainActivity" >    <item android:title="file">           <menu>            <group android:id="@+id/file_group"                android:checkableBehavior="none">                <item android:id="@+id/newfile"                    android:title="new"                    android:alphabeticShortcut="n">    <!-- 添加快速鍵-->                </item>                                <item android:id="@+id/openfile"                    android:title="open"                    android:alphabeticShortcut="o">                </item>                               <item android:id="@+id/closefile"                    android:title="close"                    android:alphabeticShortcut="c">                </item>                          </group>        </menu>           </item>        <item android:title="edit"        android:icon="@drawable/pwordpress">   <!--設定表徵圖 -->        <menu>            <group android:id="@+id/edit_group"                android:checkableBehavior="single">                <item android:id="@+id/cut"                    android:title="cut"                    android:alphabeticShortcut="t">                </item>                                <item android:id="@+id/copy"                    android:title="copy"                    android:alphabeticShortcut="p">                </item>                               <item android:id="@+id/paste"                    android:title="paste"                    android:alphabeticShortcut="a">                </item>                          </group>        </menu>           </item>        <item android:title="about"        android:icon="@drawable/pyahoo">           <menu>            <group android:id="@+id/about_group">                <item android:id="@+id/help"                    android:title="help">                </item>                                <item android:id="@+id/exit"                    android:title="exit">                </item>                                       </group>        </menu>           </item></menu>


在.java中引用,在.java中實現函數public boolean onCreatOptionsMenu(Menu)函數顯示菜單

public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}

在.java中重寫public boolean onOptionsItemSelected(MenuItem item)函數相應菜單事件

public boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.copy) {return true;}return super.onOptionsItemSelected(item);}


本文出自 “無用大叔” 部落格,請務必保留此出處http://aslonely.blog.51cto.com/6552465/1614840

Android 資源詳解(二) 菜單資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.