Android學(xué)習(xí)筆記——Menu介紹(三)
知識(shí)點(diǎn)
今天繼續(xù)昨天沒有講完的Menu的學(xué)習(xí),主要是Popup Menu的學(xué)習(xí)。
Popup Menu(彈出式菜單)
彈出式菜單是一種固定在View上的菜單模型。主要用于以下三種情況:
為特定的內(nèi)容提供溢出風(fēng)格(overflow-style)的菜單進(jìn)行操作。
提供其他部分的命令句(command sentence)如Add按鈕可以用彈出菜單提供不同的Add的操作。
提供類似于Spinner的下拉式菜單但不保持持久的選擇。
那怎樣顯示彈出式菜單呢?
如果你在XML文件中定義了菜單,那么以下三步就可顯示:
1.用PopupMenu的構(gòu)造器實(shí)例化彈出式菜單,需要當(dāng)前應(yīng)用的Context和菜單需要固定到的View。
2.使用MenuInflater填充你的菜單資源到Menu對(duì)象中,這個(gè)Menu對(duì)象是由PopupMenu.getMenu返回的(在API 14和以上 可以用PopupMenu.inflater替代)
3.調(diào)用PopupMenu.show()
下面通過(guò)一個(gè)例子來(lái)理解PopupMenu的使用:
public void showPopup(View v){ PopupMenu popup = new PopupMenu(this,v); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.popup, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Override public boolean onMenuItemClick(MenuItem arg0) { switch (arg0.getItemId()) { case R.id.item1: Toast.makeText(this, "you have clicked the item 1", Toast.LENGTH_LONG).show(); break; case R.id.item2: Toast.makeText(this, "you have clicked the item 2", Toast.LENGTH_LONG).show(); break; case R.id.item3: Toast.makeText(this, "you have clicked the item 3", Toast.LENGTH_LONG).show(); break; default: break; } return false; }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clickMe" android:onClick="showPopup" android:clickable="true"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:clickable="true" android:onClick="showPopup" /> </LinearLayout>
- Android動(dòng)態(tài)添加menu菜單的簡(jiǎn)單方法
- 用Android MenuInflater創(chuàng)建菜單項(xiàng)的方法步驟
- Android創(chuàng)建Menu菜單實(shí)例
- Android提高之自定義Menu(TabMenu)實(shí)現(xiàn)方法
- Android開源組件SlidingMenu側(cè)滑菜單使用介紹
- Android學(xué)習(xí)筆記——Menu介紹(二)
- 在Android中創(chuàng)建菜單項(xiàng)Menu以及獲取手機(jī)分辨率的解決方法
- Android仿微信菜單(Menu)(使用C#和Java分別實(shí)現(xiàn))
- Android Menu半透明效果的開發(fā)實(shí)例
相關(guān)文章
Android屬性動(dòng)畫之ValueAnimator代碼詳解
這篇文章主要介紹了Android屬性動(dòng)畫之ValueAnimator代碼詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Android四種數(shù)據(jù)存儲(chǔ)的應(yīng)用方式
這篇文章主要介紹了Android四種數(shù)據(jù)存儲(chǔ)的應(yīng)用方式的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握Android存儲(chǔ)數(shù)據(jù)的方法,需要的朋友可以參考下2017-10-10Eclipse下配置Ant腳本并自動(dòng)打包帶簽名的Android apk
這篇文章主要介紹了Eclipse下配置Ant腳本并自動(dòng)打包帶簽名的Android apk的相關(guān)資料,需要的朋友可以參考下2016-03-03Android音頻處理之通過(guò)AudioRecord去保存PCM文件進(jìn)行錄制,播放,停止,刪除功能
這篇文章主要介紹了Android音頻處理之通過(guò)AudioRecord去保存PCM文件進(jìn)行錄制,播放,停止,刪除功能的相關(guān)資料,需要的朋友可以參考下2016-11-11Android開發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能,涉及Android基于ImageView的攝像頭拍攝圖片加載、保存及權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Android將Xamarin For VS升級(jí)為4.1.0.530版教程
這篇文章主要介紹了Android將Xamarin For VS升級(jí)為4.1.0.530版的圖文教程,感興趣的小伙伴們可以參考一下2016-06-06Android中Item實(shí)現(xiàn)點(diǎn)擊水波紋效果
這篇文章主要給大家介紹了關(guān)于Android中Item實(shí)現(xiàn)點(diǎn)擊水波紋效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11