弹出式菜单主要是显示在按钮旁边的菜单主键
主要分为四步:这里以添加了按钮控件为例
第一步:实例化对象
//1.实例化PopupMenu对象(参数2:被锚定的view)
PopupMenu menu = new PopupMenu(MainActivity.this, popupButton);
第二步:加载菜单的资源
//2.加载菜单资源:利用MenuInflate将menu资源加载到PopupMenu.getMenu()所返回的对象中 //将R.menu.xx对于的菜单资源加载到弹出式菜单中 menu.getMenuInflater().inflate(R.menu.popup, menu.getMenu());
第三步:对于选项设置点击监听器
//3.PopupMenu设置点击监听器 menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.copy: Toast.makeText(MainActivity.this, "复制", Toast.LENGTH_LONG).show(); break; case R.id.paste: Toast.makeText(MainActivity.this, "粘贴", Toast.LENGTH_LONG).show(); break; } return false; } });
第四步:进行menu的展示
//4.千万不要忘记这一步,显示PopupMenu menu.show();
完整java代码
package com.example.imoocapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.ActionMode; import android.view.ContextMenu; import android.view.Menu; 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 popupButton = findViewById(R.id.popup_btn); findViewById(R.id.popup_btn).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view){ //1.实例化PopupMenu对象(参数2:被锚定的view) PopupMenu menu = new PopupMenu(MainActivity.this, popupButton); //2.加载菜单资源:利用MenuInflate将menu资源加载到PopupMenu.getMenu()所返回的对象中 //将R.menu.xx对于的菜单资源加载到弹出式菜单中 menu.getMenuInflater().inflate(R.menu.popup, menu.getMenu()); //3.PopupMenu设置点击监听器 menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.copy: Toast.makeText(MainActivity.this, "复制", Toast.LENGTH_LONG).show(); break; case R.id.paste: Toast.makeText(MainActivity.this, "粘贴", Toast.LENGTH_LONG).show(); break; } return false; } }); //4.千万不要忘记这一步,显示PopupMenu menu.show(); } }); } }
popup.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="复制" android:id="@+id/copy"/> <item android:title="粘贴" android:id="@+id/paste"/> </menu>
activity.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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/ctx_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上下文菜单" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/popup_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出式菜单" app:layout_constraintTop_toBottomOf="@+id/ctx_btn" tools:layout_editor_absoluteX="16dp" /> </androidx.constraintlayout.widget.ConstraintLayout>