Android中创建PopupMenu弹出式菜单

之前写过一篇创建option menu的文章:Android中创建option menu

本文主要是讲如何创建PopupMenu弹出式菜单

1、首先创建menu文件menu2.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/item2"
        android:title="Item 2" />
</menu>

这个menu2.xml菜单文件在Android Studio工具中可以通过可视化界面创建,拖两个Menu Item到界面中即可

 

2、创建Activity活动

界面上放一个按钮,点击按钮时填出菜单

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</android.support.constraint.ConstraintLayout>

 

MainActivity.java

package com.example.chenrui.app1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(MainActivity.this,v);
                popupMenu.getMenuInflater().inflate(R.menu.menu2,popupMenu.getMenu());

                //弹出式菜单的菜单项点击事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.item1:
                                Toast.makeText(MainActivity.this, "item 1 click.", Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.item2:
                                Toast.makeText(MainActivity.this, "item 2 click.", Toast.LENGTH_SHORT).show();
                        }
                        return false;
                    }
                });

                //弹出式菜单的菜单的关闭事件
                popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                    @Override
                    public void onDismiss(PopupMenu menu) {
                        Toast.makeText(MainActivity.this, "menu close.", Toast.LENGTH_SHORT).show();
                    }
                });
                popupMenu.show();
            }
        });
    }
} 

 

显示的效果:

 

posted @ 2019-01-17 12:00  魔豆  阅读(1412)  评论(0编辑  收藏  举报