Popup menu:弹出式菜单

唐高卓
2023-12-01
          case R.id.bt_menu:
                //创建弹出式菜单对象(最低版本11)
                PopupMenu popup = new PopupMenu(this, v);//第二个参数是绑定的那个view
                //获取菜单填充器
                MenuInflater inflater = popup.getMenuInflater();
                //填充菜单
                inflater.inflate(R.menu.main, popup.getMenu());
                //绑定菜单项的点击事件
                popup.setOnMenuItemClickListener(this);
                //显示(这一行代码不要忘记了)
                popup.show();

                break;

记得在这个方法中处理逻辑

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        String title = (String) item.getTitle();
        String styeByTitle = getStyeByTitle(title);
        loadMap(mSavedInstanceState, styeByTitle);
        return super.onOptionsItemSelected(item);
    }

R.menu.main

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <item
        android:id="@+id/MAPBOX_STREETS"
        android:title="街道"></item>

    <item
        android:id="@+id/OUTDOORS"
        android:title="户外"></item>

    <item
        android:id="@+id/LIGHT"
        android:title="亮"></item>
    <item
        android:id="@+id/DARK"
        android:title="暗"></item>

    <item
        android:id="@+id/SATELLITE"
        android:title="卫星"></item>

    <item
        android:id="@+id/SATELLITE_STREETS"
        android:title="卫星街道"></item>

    <item
        android:id="@+id/TRAFFIC_NIGHT"
        android:title="交通夜晚"></item>
    <item
        android:id="@+id/TRAFFIC_DAY"
        android:title="交通白天"></item>

</menu>

 

 类似资料: