欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

基于Android实现点击某个按钮让菜单选项从按钮周围指定位置弹出

程序员文章站 2023-12-16 21:02:28
android material design:popupmenu android material design 引入的popupmenu类似过去的上下文菜单,但是...

android material design:popupmenu

android material design 引入的popupmenu类似过去的上下文菜单,但是更灵活。 如图所示:

基于Android实现点击某个按钮让菜单选项从按钮周围指定位置弹出

现在给出实现上图popupmenu的代码。 本例是一个普通的button触发弹出popupmenu。

测试的mainactivity.java :

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:

<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实现点击某个按钮让菜单选项从按钮周围指定位置弹出的相关知识,希望大家喜欢。

上一篇:

下一篇: