在Android開發中,經常需要通過點擊某個按鈕彈出對話框或者選擇框,通過Dialog或者PopupMenu、PopupWindow都能實現。
這里主要介紹后兩者:PopupMenu、PopupWindow的實現。 先看兩個效果圖上邊PopupMenu,下邊PopupWindow:
PopupMenu PopupWindow


一、PopupMenu實現:
PopupMenu實現起來比較簡單,主要用來實現根據按鈕附近彈出的對話框。
首先定義一個menu文件/res/menu/headmenu.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" tools:context="com.arbo.hero.LoginActivity"> <item android:id="@+id/camera" android:title="拍照" android:orderInCategory="100" app:showAsAction="never" /> <item android:id="@+id/gallery" android:title="從相冊中選取" android:orderInCategory="100" app:showAsAction="never" /> <item android:id="@+id/cancel" android:title="取消" android:orderInCategory="100" app:showAsAction="never" /></menu>
創建一個PopupMenu并添加點擊事件:
private void showPopmenu(View view){ popupMenu = new PopupMenu(this,view); popupMenu.getMenuInflater().inflate(R.menu.headmenu,popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch(item.getItemId()){ case R.id.camera: Toast.makeText(HeadPortrait.this,"Click camera",Toast.LENGTH_SHORT).show(); break; case R.id.gallery: Toast.makeText(HeadPortrait.this,"Click gallery",Toast.LENGTH_SHORT).show(); break; case R.id.cancel: Toast.makeText(HeadPortrait.this,"Click cancel",Toast.LENGTH_SHORT).show(); break; } return false; } }); popupMenu.show(); }MainActivity很簡單,點擊按鈕調用showPopmenu()方法即可:
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //main.xml頁面主布局只有一個按鈕,這里就不貼代碼了 setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //點擊按鈕就創建并顯示一個popupMenu showPopmenu(btnmenu); } } }}以上,就實現了利用PopupMenu在按鈕附近彈出一個選擇框。
PopupMenu的優點:簡單;根據菜單大小自適應位置,在按鈕附近彈出;適合某些情景。
缺點:形式比較單一,效果一般。
二、PopupWindow實現:
相比之下,PopupWindow的實現復雜一些,但是自定義性更強,它可以將任意界面設置為PopupWindow。
先看彈出window布局window_popup.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:background="#dadada" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/camera" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照" android:background="#f0f0f0" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#2d2c2c" /> <Button android:background="#f0f0f0" android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="從手機相冊選擇"/> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#2d2c2c" /> <Button android:background="#f0f0f0" android:id="@+id/savepicture" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存圖片"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="vertical"> <Button android:background="#f0f0f0" android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout></LinearLayout>
布局的效果圖:

創建popupWindow并為其添加點擊事件:
void bottomwindow(View view) { if (popupWindow != null && popupWindow.isShowing()) { return; } LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.window_popup, null); popupWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //點擊空白處時,隱藏掉pop窗口 popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); //添加彈出、彈入的動畫 popupWindow.setAnimationStyle(R.style.Popupwindow); int[] location = new int[2]; view.getLocationOnScreen(location); popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]); //添加按鍵事件監聽 setButtonListeners(layout); //添加pop窗口關閉事件,主要是實現關閉時改變背景的透明度 popupWindow.setOnDismissListener(new poponDismissListener()); backgroundAlpha(1f); }事件監聽的函數setButtonListeners() :
private void setButtonListeners(LinearLayout layout) { Button camera = (Button) layout.findViewById(R.id.camera); Button gallery = (Button) layout.findViewById(R.id.gallery); Button savepicture = (Button) layout.findViewById(R.id.savepicture); Button cancel = (Button) layout.findViewById(R.id.cancel); camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (popupWindow != null && popupWindow.isShowing()) { //在此處添加你的按鍵處理 xxx popupWindow.dismiss(); } } }); gallery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (popupWindow != null && popupWindow.isShowing()) { //在此處添加你的按鍵處理 xxx popupWindow.dismiss(); } } }); savepicture.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (popupWindow != null && popupWindow.isShowing()) { //在此處添加你的按鍵處理 xxx popupWindow.dismiss(); } } }); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } } }); }彈出、收回的動畫:
若res文件夾下沒有anim目錄,則自己添加一個:new 主站蜘蛛池模板: 景德镇市| 特克斯县| 镇康县| 集贤县| 东源县| 黄山市| 嘉黎县| 上杭县| 德格县| 马边| 武平县| 个旧市| 介休市| 漳浦县| 桃源县| 长宁区| 延寿县| 辽源市| 五原县| 阿瓦提县| 游戏| 额敏县| 平潭县| 页游| 吉安市| 无锡市| 大名县| 大同县| 阿克苏市| 沁源县| 天门市| 定襄县| 慈溪市| 兴安县| 遂宁市| 乳源| 宁明县| 长沙县| 长丰县| 武邑县| 志丹县|