Android學(xué)習(xí)筆記——Menu介紹(三)
知識點
今天繼續(xù)昨天沒有講完的Menu的學(xué)習(xí),主要是Popup Menu的學(xué)習(xí)。
Popup Menu(彈出式菜單)
彈出式菜單是一種固定在View上的菜單模型。主要用于以下三種情況:
為特定的內(nèi)容提供溢出風(fēng)格(overflow-style)的菜單進行操作。
提供其他部分的命令句(command sentence)如Add按鈕可以用彈出菜單提供不同的Add的操作。
提供類似于Spinner的下拉式菜單但不保持持久的選擇。
那怎樣顯示彈出式菜單呢?
如果你在XML文件中定義了菜單,那么以下三步就可顯示:
1.用PopupMenu的構(gòu)造器實例化彈出式菜單,需要當(dāng)前應(yīng)用的Context和菜單需要固定到的View。
2.使用MenuInflater填充你的菜單資源到Menu對象中,這個Menu對象是由PopupMenu.getMenu返回的(在API 14和以上 可以用PopupMenu.inflater替代)
3.調(diào)用PopupMenu.show()
下面通過一個例子來理解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>
相關(guān)文章
Android四種數(shù)據(jù)存儲的應(yīng)用方式
這篇文章主要介紹了Android四種數(shù)據(jù)存儲的應(yīng)用方式的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握Android存儲數(shù)據(jù)的方法,需要的朋友可以參考下2017-10-10Eclipse下配置Ant腳本并自動打包帶簽名的Android apk
這篇文章主要介紹了Eclipse下配置Ant腳本并自動打包帶簽名的Android apk的相關(guān)資料,需要的朋友可以參考下2016-03-03Android音頻處理之通過AudioRecord去保存PCM文件進行錄制,播放,停止,刪除功能
這篇文章主要介紹了Android音頻處理之通過AudioRecord去保存PCM文件進行錄制,播放,停止,刪除功能的相關(guān)資料,需要的朋友可以參考下2016-11-11Android開發(fā)實現(xiàn)ImageView加載攝像頭拍攝的大圖功能
這篇文章主要介紹了Android開發(fā)實現(xiàn)ImageView加載攝像頭拍攝的大圖功能,涉及Android基于ImageView的攝像頭拍攝圖片加載、保存及權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Android將Xamarin For VS升級為4.1.0.530版教程
這篇文章主要介紹了Android將Xamarin For VS升級為4.1.0.530版的圖文教程,感興趣的小伙伴們可以參考一下2016-06-06