Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 基於Android實現點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出

基於Android實現點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出

編輯:關於Android編程

Android Material Design:PopupMenu

Android Material Design 引入的PopupMenu類似過去的上下文菜單,但是更靈活。 如圖所示:

現在給出實現上圖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實現點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出的相關知識,希望大家喜歡。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved