android各種菜單使用介紹

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   使用   ar   for   

Android菜單的有這幾種:

  1,OptionMenue:選項菜單
  2,contextMenu:操作功能表
  3,SubMenu子功能表

其中,OptionMenue與contextMenu的區別(OptionMenue與SubMenu區別較小):
  1,OptionMenue對應的四activity,一個activity只能擁有一個選項菜單
  2,contextMenu對於的是View,每一個View都可以設定操作功能表
  3,一般情況下contextMenu常用於ListView或者GridView;

下面分別進行詳細的介紹:

第一:OptionMenue:選項菜單,

  1):菜單的實現:

    在activity中實現方法:onCreateOptionsMenu()

    1.1:設定功能表項目可用代碼動態設定menuadd();
        1.2:通過xml設定,getMenuInflater().inflate

  2):監聽的實現:

    在activity中實現方法:onCreateOptionsMenu()

 

    樣本如下:

    @Override    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;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        int id = item.getItemId();        switch (id) {        case R.id.action_menu_item1:            //設定跳轉介面            Intent intent = new Intent(MainActivity.this, SettingActivity.class);            item.setIntent(intent);            //Toast.makeText(this, "點擊了菜單1", 0).show();            break;        case R.id.action_menu_item2:            Toast.makeText(this, "點擊了菜單2", 0).show();            break;        }                return super.onOptionsItemSelected(item);    }

其中main.xml的實現如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.example.optionmenu.MainActivity" >    <item        android:id="@+id/action_menu_item1"        android:orderInCategory="100"        android:showAsAction="never"        android:title="設定"/>    <item        android:id="@+id/action_menu_item2"        android:orderInCategory="100"        android:showAsAction="never"        android:title="選項"/></menu>

 

第二:contextMenu:操作功能表(windows下右鍵的彈出,Android下針對檔案長按):

構成:表徵圖,標題,功能表項目,監聽

建立方法:
  1,在View註冊操作功能表registerForContextMenu()
  2,添加操作功能表內容onCreateContextMenu()
    2.1:可以通過代碼動態添加
    2.2:可以載入xml檔案夾中的功能表項目
  3,設定菜單點擊後的響應事件onContenxtItemSelected();

樣本:mainActivity的實現如下:

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        showListView();    }  //設定展示的列表    private void showListView(){        ListView listView = (ListView) findViewById(R.id.listview);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_1, getData());        listView.setAdapter(adapter);        this.registerForContextMenu(listView);//設定菜單選項    }
   //為列表設定資料
private ArrayList<String> getData(){ ArrayList<String> list = new ArrayList<String>(); for(int i=0;i<15;i++){ list.add("列表"+i); } return list; } @Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //設定菜單顯示內容 menu.setHeaderTitle("操作");
  //方法二:通過xml進行選項的設定 menu.setHeaderIcon(R.drawable.ic_launcher);
  //方法一:通過add動態添加
// menu.add(1, 1, 1, "複製");// menu.add(1, 2, 1, "粘貼");// menu.add(1, 3, 1, "剪下");// menu.add(1, 4, 1, "重新命名"); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu);} @Overridepublic boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this, "點擊複製", 0).show(); break; case 2: Toast.makeText(this, "點擊粘貼", 0).show(); break; case 3: Toast.makeText(this, "點擊剪下", 0).show(); break; case 4: Toast.makeText(this, "點擊重新命名", 0).show(); break; } return super.onContextItemSelected(item);} }

 

第三:SubMenu子功能表:

這個實現起來和OptionMenue差不多,所需要實現的方法也是一致的,只是需要在onCreateOptionsMenu()方法中定義SubMenu

對象,

具體樣本如下:

 

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    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);        SubMenu file = menu.addSubMenu("檔案");        SubMenu edit = menu.addSubMenu("編輯");        file.add(1, 1, 1, "建立");        file.add(1, 2, 1, "開啟");        file.add(1, 3, 1, "儲存");        file.setHeaderTitle("檔案操作");        file.setHeaderIcon(R.drawable.ic_launcher);        edit.add(2, 1, 1, "複製");        edit.add(2, 2, 1, "粘貼");        edit.add(2, 3, 1, "剪下");        edit.add(2, 4, 1, "重新命名");        edit.setHeaderTitle("編輯操作");        edit.setHeaderIcon(R.drawable.ic_launcher);        return true;    }    @Override    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.        if(item.getGroupId()==1){            switch (item.getItemId()) {            case 1:                Toast.makeText(this, "點擊建立", 0).show();                 break;            case 2:                Toast.makeText(this, "點擊開啟", 0).show();                 break;            case 3:                Toast.makeText(this, "點擊儲存", 0).show();                 break;            }        }else if(item.getGroupId()==2){            switch (item.getItemId()) {             case 1:                Toast.makeText(this, "點擊複製", 0).show();                break;             case 2:                Toast.makeText(this, "點擊粘貼", 0).show();                break;             case 3:                Toast.makeText(this, "點擊剪下", 0).show();                break;            case 4:                Toast.makeText(this, "點擊重新命名", 0).show();                 break;            }        }        return super.onOptionsItemSelected(item);    }}

 

Darren

微博:@IT_攻城師

github:@Darren90

出處:http://www.cnblogs.com/fengtengfei/

 

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.