<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="white-space:pre"> </span>今天給大家帶來的Menu的多種使用方式</span>
在創建一個應用的時候就會看到在MainActivity中會有這個類方法
@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;
}
獲取加載器,加載你寫的Menu.xml,返回ture;
這是最基本的使用方式,然而在開發根本不能滿足我們的需求,今天給大家帶來常用的幾種方式
最常用的方式就是在menu文件夾中寫入自己想要的佈局
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Options First"/>
<item
android:id="@+id/action_settings2"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Optons Second"/>
</menu>
這是一種以xml的方式
除了這種方法我們還可以動態的添加Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//xml 形式添加Menu
// getMenuInflater().inflate(R.menu.main, menu);
//動態添加Options
MenuItem item = menu.add(1, 10, 1, "Options First");
item.setTitle("Options First 2");
menu.add(1, 11, 1, "Options Second");
menu.add(1, 12, 1, "Options Three");
menu.add(1, 13, 1, "Options Four");
return true;
}
其中使用menu裏面的add()方法即可添加,其中第一個參數爲MenuGroupId,第二個參數爲id, 第三個參數爲排序方式, 第四個參數爲Menu的名稱
MenuItem item = menu.add(1, 10, 1, "Options First");
item.setTitle("Options First 2");
獲取到Menuitem,還可以設置標題,在 sdk > 11的時候可以添加圖標但是現在不行了
接下來設置menu點擊後相應時間,其中需要在裏面重寫onOptionsItemSelected(MenuItem item)方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
/* switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "Onclick Options First", Toast.LENGTH_LONG).show();
break;
case R.id.action_settings2:
Toast.makeText(getApplicationContext(), "Onclick Options Second", Toast.LENGTH_LONG).show();
break;
default:
break;
}*/
switch (item.getItemId()) {
case 10:
// item.setIntent(arg0); 跳轉到某界面上去
Toast.makeText(getApplicationContext(), "Onclick Options First", Toast.LENGTH_LONG).show();
break;
case 11:
Toast.makeText(getApplicationContext(), "Onclick Options Second", Toast.LENGTH_LONG).show();
break;
case 12:
Toast.makeText(getApplicationContext(), "Onclick Options Three", Toast.LENGTH_LONG).show();
break;
case 13:
Toast.makeText(getApplicationContext(), "Onclick Options Four", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
我們在裏面可以實現頁面的跳轉,在裏面可以獲取到MenuItem的setItemid() or setIntent();第二種方式設置 : ContextMenu
相信大家都是用過文件管理器,長按的狀態下,會觸發另外一種Menu,而這種Menu就是ContextMenu
首先我們需要在View註冊registerForContextMenu()
然後添加上下文菜單內容onCreateContextMenu()
-- 通過加載的方式添加menu,xml
-- 通過menu動態的添加菜單項
設置菜單後點擊菜單的響應事件onContextItemSelected();
private void showListview() {
// TODO Auto-generated method stub
ListView listView = (ListView) findViewById(R.id.listviewid);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
getdata());
listView.setAdapter(adapter);
// 註冊上下文菜單
this.registerForContextMenu(listView);
}
private ArrayList<String> getdata() {
// TODO Auto-generated method stub
ArrayList<String> arrayList = new ArrayList<String>();
for (int i = 0; i < 5; ++i) {
arrayList.add("文件" + (i + 1));
}
return arrayList;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.setHeaderTitle("Menu");
menu.setHeaderIcon(R.drawable.ic_launcher);
// //靜態註冊
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.main, menu);
menu.add(1, 1, 1, "copy");
menu.add(1, 2, 1, "paste");
menu.add(1, 3, 1, "rename");
menu.add(1, 4, 1, "delete");
super.onCreateContextMenu(menu, v, menuInfo);
}
在Menu.xml添加Menu菜單
MenuInflater inflater = getMenuInflater();
nflater.inflate(R.menu.main, menu);
設置點擊Menu事件:
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
Toast.makeText(getApplicationContext(), "Onclick a copy",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "Onclick a paste",
Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(getApplicationContext(), "Onclick a rename",
Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(getApplicationContext(), "Onclick a delete",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
第三種方法 : SubMenu
子項菜單
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);
// add second SunMenu
SubMenu file = menu.addSubMenu("File");
SubMenu edit = menu.addSubMenu("Edit");
file.add(1, 1, 1, "Copy");
file.add(1, 2, 1, "Paste");
file.add(1, 3, 1, "Rename");
file.add(1, 4, 1, "Delete");
file.setHeaderIcon(R.drawable.ic_launcher);
file.setHeaderTitle("File Operation");
edit.add(2, 1, 1, "EditTitle");
edit.add(2, 2, 1, "EditContext");
edit.setHeaderIcon(R.drawable.ic_launcher);
edit.setHeaderTitle("Edit Operation");
return true;
}
使用menu.subMenu(“menu名稱”),從而獲取subMenu方法,使用SubMenu.add在添加它的子Menu,
file.setHeaderIcon(R.drawable.ic_launcher);
file.setHeaderTitle("File Operation");
設置它的標題和圖片
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getGroupId() == 1) {
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this, "You Onclick Copy",
Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(MainActivity.this, "You Onclick Paste",
Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(MainActivity.this, "You Onclick Rename",
Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(MainActivity.this, "You Onclick Delete",
Toast.LENGTH_LONG).show();
break;
}
} else if (item.getGroupId() == 2) {
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this, "You Onclick EditTitle",
Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(MainActivity.this, "You Onclick EditContext",
Toast.LENGTH_LONG).show();
break;
}
}
return super.onContextItemSelected(item);
}
看到這裏調用了getGroupId()方法,這個方法獲取到前面所說的MenuGroupId,然後在對子項菜單進行id判斷我們需要在AndroidManifest.xml裏面添加上,從而隱藏掉佈局右上角的三個點
android:theme="@android:style/Theme.Light.NoTitleBar"
今天爲大家介紹了這幾種的Menu的常用方式,希望大家多多支持我,謝謝