實話說 就個人感覺 Memu 真的很丑 所以今天嘗試換掉之
?
?
[原理]
1. 保留Menu按鍵起作用
2. 用ListView顯示所有子菜單
?
?
?
[代碼 步驟]
1. 定義 menu.xml 僅含有一個ListView 并得到其View
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/mList" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
?
mview = this.getLayoutInflater().inflate(R.layout.menu, null);
?
?
2. 使用PopupWindow作為菜單顯示容器 并得到其中的ListView 供后續使用
mpop = new PopupWindow(mview,500,200); mpop.setFocusable(true); mlist = (ListView)mview.findViewById(R.id.mList);
?
?
3. 定義List<Map<String,String>> moption 用于存放菜單名字
public void initialMenu(){ moption = new ArrayList<Map<String,String>>(); Map<String,String> map1 = new HashMap<String,String>(); map1.put("Option", "新增"); moption.add(map1); Map<String,String> map2 = new HashMap<String,String>(); map2.put("Option", "刪除"); moption.add(map2); Map<String,String> map3 = new HashMap<String,String>(); map3.put("Option", "屬性"); moption.add(map3); }
?
4. 在ListView中顯示moption
String[] from = {"Option"}; int[] to = {R.id.text1}; madapter = new SimpleAdapter(this, moption, R.layout.menulayout, from,to); mlist.setAdapter(madapter);
?
5. 注冊Menu彈起事件
public boolean onCreateOptionsMenu(Menu menu) { mpop.showAtLocation(findViewById(R.id.mainlayout), Gravity.CENTER, 30, 40); return false; }
?
6. 注冊Menu子項單擊事件 而后關閉之
mlist.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub doMenu(arg2); mpop.dismiss(); } }); public void doMenu(int i){ switch(i){ case 0: break; case 1: break; } Toast.makeText(this, "Menu "+i+" is selected!", Toast.LENGTH_LONG).show(); }
?
?
?7. emulator 運行截圖:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
