你的位置:首页 > 操作系统

[操作系统]点击某个按钮,让菜单选项从按钮周围指定位置弹出。


 

Android Material Design:PopupMenu

Android Material Design 引入的PopupMenu类似过去的上下文菜单,但是更灵活。 如图所示:

 

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

测试的MainActivity.java :

[java] view plaincopyprint?

  1. package zhangphil.materialdesign;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.support.v7.widget.PopupMenu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.   
  18.         final Button button = (Button) findViewById(R.id.button);  
  19.         button.setOnClickListener(new View.OnClickListener() {  
  20.   
  21.             @Override  
  22.             public void onClick(View v) {  
  23.                 showPopupMenu(button);  
  24.             }  
  25.         });  
  26.     }  
  27.   
  28.     private void showPopupMenu(View view) {  
  29.         // View当前PopupMenu显示的相对View的位置  
  30.         PopupMenu popupMenu = new PopupMenu(this, view);  
  31.   
  32.         // menu布局  
  33.         popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());  
  34.   
  35.         // menu的item点击事件  
  36.         popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
  37.             @Override  
  38.             public boolean onMenuItemClick(MenuItem item) {  
  39.                 Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();  
  40.                 return false;  
  41.             }  
  42.         });  
  43.   
  44.         // PopupMenu关闭事件  
  45.         popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {  
  46.             @Override  
  47.             public void onDismiss(PopupMenu menu) {  
  48.                 Toast.makeText(getApplicationContext(), "关闭PopupMenu", Toast.LENGTH_SHORT).show();  
  49.             }  
  50.         });  
  51.   
  52.         popupMenu.show();  
  53.     }  
  54. }  

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.[html] view plaincopyprint?

  1. <menu "http://schemas.android.com/apk/res/android"  
  2.     "http://schemas.android.com/apk/res-auto"  
  3.     "http://schemas.android.com/tools" >  
  4.   
  5.     <item  
  6.         android:id="@+id/action_open"  
  7.         android:orderInCategory="100"  
  8.         android:title="打开"  
  9.         app:showAsAction="never"/>  
  10.     <item  
  11.         android:id="@+id/action_new"  
  12.         android:orderInCategory="100"  
  13.         android:title="新建"  
  14.         app:showAsAction="never"/>  
  15.     <item  
  16.         android:id="@+id/action_del"  
  17.         android:orderInCategory="100"  
  18.         android:title="删除"  
  19.         app:showAsAction="never"/>  
  20.   
  21. </menu>