android material design:popupmenu
android material design 引入的popupmenu类似过去的上下文菜单,但是更灵活。 如图所示:
现在给出实现上图popupmenu的代码。 本例是一个普通的button触发弹出popupmenu。
测试的mainactivity.java :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
package zhangphil.materialdesign;
import android.app.activity;
import android.os.bundle;
import android.support.v.widget.popupmenu;
import android.view.menuitem;
import android.view.view;
import android.widget.button;
import android.widget.toast;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
final button button = (button) findviewbyid(r.id.button);
button.setonclicklistener( new view.onclicklistener() {
@override
public void onclick(view v) {
showpopupmenu(button);
}
});
}
private void showpopupmenu(view view) {
// view当前popupmenu显示的相对view的位置
popupmenu popupmenu = new popupmenu( this , view);
// menu布局
popupmenu.getmenuinflater().inflate(r.menu.main, popupmenu.getmenu());
// menu的item点击事件
popupmenu.setonmenuitemclicklistener( new popupmenu.onmenuitemclicklistener() {
@override
public boolean onmenuitemclick(menuitem item) {
toast.maketext(getapplicationcontext(), item.gettitle(), toast.length_short).show();
return false ;
}
});
// popupmenu关闭事件
popupmenu.setondismisslistener( new popupmenu.ondismisslistener() {
@override
public void ondismiss(popupmenu menu) {
toast.maketext(getapplicationcontext(), "关闭popupmenu" , toast.length_short).show();
}
});
popupmenu.show();
}
}
package zhangphil.materialdesign;
import android.app.activity;
import android.os.bundle;
import android.support.v7.widget.popupmenu;
import android.view.menuitem;
import android.view.view;
import android.widget.button;
import android.widget.toast;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
final button button = (button) findviewbyid(r.id.button);
button.setonclicklistener( new view.onclicklistener() {
@override
public void onclick(view v) {
showpopupmenu(button);
}
});
}
private void showpopupmenu(view view) {
// view当前popupmenu显示的相对view的位置
popupmenu popupmenu = new popupmenu( this , view);
// menu布局
popupmenu.getmenuinflater().inflate(r.menu.main, popupmenu.getmenu());
// menu的item点击事件
popupmenu.setonmenuitemclicklistener( new popupmenu.onmenuitemclicklistener() {
@override
public boolean onmenuitemclick(menuitem item) {
toast.maketext(getapplicationcontext(), item.gettitle(), toast.length_short).show();
return false ;
}
});
// popupmenu关闭事件
popupmenu.setondismisslistener( new popupmenu.ondismisslistener() {
@override
public void ondismiss(popupmenu menu) {
toast.maketext(getapplicationcontext(), "关闭popupmenu" , toast.length_short).show();
}
});
popupmenu.show();
}
}
|
mainactivity.java需要的activity_main布局文件只含一个普通的android button,在次不再赘述。
位于menu目录下的main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<menu xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:app= "http://schemas.android.com/apk/res-auto"
xmlns:tools= "http://schemas.android.com/tools" >
<item
android:id= "@+id/action_open"
android:orderincategory="
android:title= "打开"
app:showasaction= "never" />
<item
android:id= "@+id/action_new"
android:orderincategory="
android:title= "新建"
app:showasaction= "never" />
<item
android:id= "@+id/action_del"
android:orderincategory="
android:title= "删除"
app:showasaction= "never" />
</menu>
|
以上内容给大家分享基于android实现点击某个按钮让菜单选项从按钮周围指定位置弹出的相关知识,希望大家喜欢。