Android PopupMenu弹出菜单的实现
程序员文章站
2023-12-15 10:03:46
android popupmenu弹出菜单的实现
(1)布局文件:用于弹出菜单的处罚button:
android popupmenu弹出菜单的实现
(1)布局文件:用于弹出菜单的处罚button:
<relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="96dp" android:layout_margintop="111dp" android:onclick="showpopup" android:text="弹出菜单" /> </relativelayout>
(2)res-menu目录下的main.xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/open" android:orderincategory="100" android:showasaction="never" android:title="@string/open"/> <item android:id="@+id/close" android:orderincategory="100" android:showasaction="never" android:title="@string/close"/> </menu>
(3)类的文件:
package com.example.menu_popupmenu; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.popupmenu; public class mainactivity extends activity { private button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void showpopup(view view) { popupmenu popupmenu = new popupmenu(mainactivity.this, view); popupmenu.getmenuinflater().inflate(r.menu.main, popupmenu.getmenu()); popupmenu.show(); } @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; } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
推荐阅读