常用的菜单
OptionsMenu (系统菜单)
OptionsMenu的使用
静态xml资源menu
1.在res资源目录创建资源文件夹menu, 并在menu中创建OptionsMenu的xml布局文件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d9c8b1a82958838ad2cac917d6ddaaad.png)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- 总是显示的item -->
<item
android:id="@+id/ew"
android:icon="@mipmap/ic_launcher"
android:title="额外啊"
app:showAsAction="always" />
<!-- 一级菜单-->
<item
android:id="@+id/item1"
android:title="渣渣辉" />
<item
android:id="@+id/item2"
android:title="古天乐" />
<item
android:id="@+id/other"
android:title="还有谁">
<!-- 一级item中的二级菜单 -->
<menu>
<item
android:id="@+id/cen"
android:title="陈小春" />
<item
android:id="@+id/gu"
android:title="古巨基" />
</menu>
</item>
</menu>
2.Activity中重写onCreateOptionsMenu方法加载资源文件
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_main, menu);
}
3.Activity重写onOptionsItemSelected加载条目选择事件监听器
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "渣渣辉", Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(this, "古天乐", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
动态代码写入OptionsMenu
1.Activity中重写onCreateOptionsMenu方法并添加菜单条目
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 添加一级菜单
menu.add(1, 1, 1, "渣渣辉");
menu.add(1, 2, 2, "古天乐");
// 添加二级菜单
SubMenu menu1 = menu.addSubMenu(1, 3, 3, "其他");
// 为二级菜单添加条目
menu1.add(2, 4, 1, "陈小春");
menu1.add(2, 5, 2, "辣稽");
return super.onCreateOptionsMenu(menu);
}
2.Activity中重写onOptionsItemSelected加载条目选择事件监听器
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
// 刚刚添加的id
case 2:
Toast.makeText(this, "噜噜噜", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
注意:一个Activity中只能有一个系统菜单
ContextMenu (上下文菜单)
ContextMenu的使用
静态xml使用ContextMenu
1.在res资源目录创建资源文件夹menu, 并在menu中创建ContextMenu的xml布局文件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/618ee2abb55435f869a72150b9f3b46d.png)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item1"
android:title="渣渣辉" />
<item
android:id="@+id/item2"
android:title="古天乐" />
<item
android:id="@+id/other"
android:title="还有谁">
<menu>
<item
android:id="@+id/cen"
android:title="陈小春" />
<item
android:id="@+id/gu"
android:title="古巨基" />
</menu>
</item>
</menu>
2.Activity中重写onCreateContextMenu方法加载资源文件
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_main, menu);
}
3.Activity中重写onOptionsItemSelected加载条目选择事件监听器
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "渣渣辉", Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(this, "古天乐", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
4.在需要使用ContextMenu的控件进行注册绑定
registerForContextMenu(View);
使用动态代码创建ContextMenu
1.Activity中重写onCreateOptionsMenu方法并添加菜单条目
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 1, "渣渣辉");
menu.add(1, 2, 2, "古天乐");
SubMenu menu1 = menu.addSubMenu(1, 3, 3, "其他");
menu1.add(2, 4, 1, "陈小春");
menu1.add(2, 5, 2, "辣稽");
return super.onCreateOptionsMenu(menu);
}
3.Activity中重写onContextItemSelected加载条目选择事件监听器
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 2:
Toast.makeText(this, "噜噜噜", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
4.在需要使用ContextMenu的控件进行注册绑定
registerForContextMenu(View);
我们可以看出其实OptionsMenu和ContextMenu用法别无两样
唯一区别就是ContextMenu需要对控件进行注册绑定,当一个视图注册了上下文菜单时,执行一个在该对象上长按的动作,将出现一个具有相关功能的浮动菜单。
PopupWindow (弹出菜单)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d5a1143faea6667ccbbb1709b905c383.png)
使用步骤
1:在res下面创建一个menu文件夹,并新建一个xml文件作为PoupMenu的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/addPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加联系人" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#aba99f" />
<LinearLayout
android:id="@+id/cleanPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空联系人" />
</LinearLayout>
</LinearLayout>
2.给需要使用PopupWindow的视图设置点击事件,并创建PopupWindow
popup.setOnClickListener(view -> {
PopupWindow popupWindow = new PopupWindow(this);
});
3.给PopupWindow设置需要使用的属性
// 设置可以响应PopupWindow外的事件
popupWindow.setOutsideTouchable(true);
// 为PopupWindow填充布局
View popWin = getLayoutInflater().inflate(R.layout.pop_menu, null);
popupWindow.setContentView(popWin);
// 可以通过此方式为菜单中的条目添加事件
popWin.findViewById(R.id.addPerson).setOnClickListener(v -> {
// 功能略。。。。。。
});
popWin.findViewById(R.id.cleanPerson).setOnClickListener(v -> {
// 功能略。。。。。。
});
/**
* PopupWindow的显示
* 1.showAsDropDown 在某个控件下面显示 构造(相对控件,x轴偏移量,y轴偏移量,对齐方式)
* 2.showAtLocation 在父级控件中显示 构造(父级控件,对齐方式,x轴偏移量,y轴偏移量)
*/
popupWindow.showAsDropDown(head, 0, 0, Gravity.RIGHT);
popupWindow.showAtLocation(main, Gravity.BOTTOM, 0, 0);
注意:弹出菜单,默认弹出的位置在控件view的下方