簡單實(shí)現(xiàn)Android彈出菜單效果
本文實(shí)例為大家分享了Android彈出菜單效果的具體代碼,供大家參考,具體內(nèi)容如下
功能描述:用戶單擊按鈕彈出菜單。當(dāng)用戶選擇一個菜單項,會觸發(fā)MenuItemClick事件并讓彈出的菜單消失;如果用戶在菜單外單擊,則直接消失彈出的菜單。當(dāng)菜單消失時,會引發(fā)DismissEvent事件(利用此事件可在菜單消失時做一些后續(xù)處理)。
1、運(yùn)行效果
2、添加菜單項
在Resources文件夾下添加一個menu子文件夾,然后在此子文件夾下添加一個名為demo07_popup_menu.xml的文件:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="item 1" /> <item android:id="@+id/item1" android:title="item 2" /> <item android:id="@+id/item1" android:title="item 3" /> </menu>
3、添加demo07_PopupMenu.axml
<?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"> <Button android:id="@+id/popupButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/showPopup" /> </LinearLayout>
保存所有文件。
4、添加Demo07PopupMenu.cs
using Android.App; using Android.OS; using Android.Widget; namespace ch05demos.SrcActivity { [Activity(Label = "Demo07PopupMenu")] public class Demo07PopupMenu : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo07_Popup); Button btn = FindViewById<Button>(Resource.Id.popupButton); btn.Click += (s, arg) => { PopupMenu menu = new PopupMenu(this, btn); menu.Inflate(Resource.Menu.demo07_popup_menu); menu.MenuItemClick += (sender, args) => { string str = string.Format("你選擇了:{0}", args.Item); Toast.MakeText(this,str, ToastLength.Short).Show(); }; menu.DismissEvent += (sender, args) => { //菜單消失后可在此事件中做一些后續(xù)處理 }; menu.Show(); }; } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- Android ListView長按彈出菜單二種實(shí)現(xiàn)方式示例
- Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android PopupWindow實(shí)現(xiàn)右側(cè)、左側(cè)和底部彈出菜單
- Android仿QQ滑動彈出菜單標(biāo)記已讀、未讀消息
- Android實(shí)現(xiàn)類似于PC中的右鍵彈出菜單效果
- Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法
- Android中微信小程序開發(fā)之彈出菜單
- Android實(shí)現(xiàn)底部彈出按鈕菜單升級版
相關(guān)文章
Android中通過訪問本地相冊或者相機(jī)設(shè)置用戶頭像實(shí)例
本篇文章主要介紹了Android中通過訪問本地相冊或者相機(jī)設(shè)置用戶頭像,具有一定的參考價值,有興趣的可以了解一下。2017-01-01http請求繞過Filter的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了http請求繞過Filter的實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06Android Gradle依賴管理、去除重復(fù)依賴、忽略的方式
這篇文章主要介紹了Android Gradle依賴管理、去除重復(fù)依賴、忽略的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android在layout xml中使用ViewStub完成動態(tài)加載問題
這篇文章主要介紹了Android在layout xml中使用ViewStub完成動態(tài)加載問題,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-08-08Android實(shí)現(xiàn)瘋狂連連看游戲之開發(fā)游戲界面(二)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)瘋狂連連看游戲之開發(fā)游戲界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03