Android之PopupWindow類似微信右上角的彈出菜單

http://blog.csdn.net/loveyaozu/article/details/51150229

日常開發過程中對於PopupWindown的使用也是比較多的。這裏給大家展示一下PopupWindow的使用。

修改activity_main.xml佈局:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="${relativePackage}.${activityClass}" >  
  6.   
  7.     <RelativeLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="50dip"  
  10.         android:background="@android:color/holo_blue_dark">  
  11.   
  12.         <ImageView  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_centerVertical="true"  
  16.             android:layout_marginLeft="10dip"  
  17.             android:background="@drawable/ic_launcher" />  
  18.   
  19.         <ImageView  
  20.             android:id="@+id/rl_more"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="match_parent"  
  23.             android:background="@drawable/ability_show_item_bg"  
  24.             android:paddingLeft="15dp"  
  25.             android:paddingRight="5dp"  
  26.             android:layout_alignParentRight="true"  
  27.             android:src="@drawable/actionbar_more_icon" />  
  28.   
  29.     </RelativeLayout>  
  30.   
  31. </RelativeLayout>  

新建popup_window.xml佈局文件
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@android:color/white"  
  6.     android:gravity="center_horizontal"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/settings"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="45dp"  
  13.         android:gravity="center"  
  14.         android:padding="12dp"  
  15.         android:text="設置"  
  16.         android:textSize="16sp" />  
  17.   
  18.     <View  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="1dp"  
  21.         android:background="#BDBDBD" />  
  22.   
  23.     <TextView  
  24.         android:id="@+id/about"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="45dp"  
  27.         android:gravity="center"  
  28.         android:padding="12dp"  
  29.         android:text="關於"  
  30.         android:textSize="16sp" />  
  31.   
  32.     <View  
  33.         android:layout_width="match_parent"  
  34.         android:layout_height="1dp"  
  35.         android:background="#BDBDBD" />  
  36.   
  37.     <TextView  
  38.         android:id="@+id/ability_logout"  
  39.         android:layout_width="match_parent"  
  40.         android:layout_height="45dp"  
  41.         android:gravity="center"  
  42.         android:padding="12dp"  
  43.         android:text="退出"  
  44.         android:textSize="16sp" />  
  45.   
  46. </LinearLayout>  

自定義PopupWindow類PopWindow
  1. package com.syz.mypopupwindow;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.drawable.ColorDrawable;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.ViewGroup.LayoutParams;  
  10. import android.widget.PopupWindow;  
  11.   
  12. /** 
  13.  * <p>Title:PopWindow</p> 
  14.  * <p>Description: 自定義PopupWindow</p> 
  15.  * @author syz 
  16.  * @date 2016-3-14 
  17.  */  
  18. public class PopWindow extends PopupWindow{  
  19.     private View conentView;  
  20.     public PopWindow(final Activity context){  
  21.         LayoutInflater inflater = (LayoutInflater) context  
  22.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  23.         conentView = inflater.inflate(R.layout.popup_window, null);  
  24.         int h = context.getWindowManager().getDefaultDisplay().getHeight();  
  25.         int w = context.getWindowManager().getDefaultDisplay().getWidth();  
  26.         // 設置SelectPicPopupWindow的View  
  27.         this.setContentView(conentView);  
  28.         // 設置SelectPicPopupWindow彈出窗體的寬  
  29.         this.setWidth(w / 2 + 40);  
  30.         // 設置SelectPicPopupWindow彈出窗體的高  
  31.         this.setHeight(LayoutParams.WRAP_CONTENT);  
  32.         // 設置SelectPicPopupWindow彈出窗體可點擊  
  33.         this.setFocusable(true);  
  34.         this.setOutsideTouchable(true);  
  35.         // 刷新狀態  
  36.         this.update();  
  37.         // 實例化一個ColorDrawable顏色爲半透明  
  38.         ColorDrawable dw = new ColorDrawable(0000000000);  
  39.         // 點back鍵和其他地方使其消失,設置了這個才能觸發OnDismisslistener ,設置其他控件變化等操作  
  40.         this.setBackgroundDrawable(dw);  
  41.         // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);  
  42.         // 設置SelectPicPopupWindow彈出窗體動畫效果  
  43.         this.setAnimationStyle(R.style.AnimationPreview);  
  44.           
  45.         conentView.findViewById(R.id.about).setOnClickListener(new OnClickListener() {  
  46.   
  47.             @Override  
  48.             public void onClick(View arg0) {  
  49.                 //do something you need here  
  50.                 PopWindow.this.dismiss();  
  51.             }  
  52.         });  
  53.         conentView.findViewById(R.id.ability_logout).setOnClickListener(new OnClickListener() {  
  54.               
  55.             @Override  
  56.             public void onClick(View arg0) {  
  57.                 // do something before signing out  
  58.                 context.finish();  
  59.                 PopWindow.this.dismiss();  
  60.             }  
  61.         });  
  62.         conentView.findViewById(R.id.settings).setOnClickListener(new OnClickListener() {  
  63.               
  64.             @Override  
  65.             public void onClick(View arg0) {  
  66.                 // do something you need here   
  67.                   
  68.                 PopWindow.this.dismiss();  
  69.             }  
  70.         });  
  71.     }  
  72.       
  73.     /** 
  74.      * 顯示popupWindow 
  75.      *  
  76.      * @param parent 
  77.      */  
  78.     public void showPopupWindow(View parent) {  
  79.         if (!this.isShowing()) {  
  80.             // 以下拉方式顯示popupwindow  
  81.             this.showAsDropDown(parent, parent.getLayoutParams().width / 25);  
  82.         } else {  
  83.             this.dismiss();  
  84.         }  
  85.     }  
  86. }  

添加自定義PopupWindow所需的style,

AnimationPreview

  1. <style name="AnimationPreview">  
  2.         <item name="android:windowEnterAnimation">@anim/fade_in</item>  
  3.         <item name="android:windowExitAnimation">@anim/fade_out</item>  
  4.     </style>  

添加style所需的動畫

fade_in.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 左上角擴大-->  
  3.   <scale   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"    
  5.         android:fromXScale="0.001"   
  6.         android:toXScale="1.0"     
  7.         android:fromYScale="0.001"     
  8.         android:toYScale="1.0"     
  9.         android:pivotX="100%"    
  10.         android:pivotY="10%"    
  11.         android:duration="200" />    
  12.      

fade_out.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 左上角縮小 -->  
  3.   <scale   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"    
  5.         android:fromXScale="1.0"     
  6.         android:toXScale="0.001"     
  7.         android:fromYScale="1.0"     
  8.         android:toYScale="0.001"     
  9.         android:pivotX="100%"    
  10.         android:pivotY="10%"   
  11.         android:duration="200" />    
  12.      


最後在MainActivity類中使用
  1. package com.syz.mypopupwindow;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7.   
  8. public class MainActivity extends Activity implements OnClickListener {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         findViewById(R.id.rl_more).setOnClickListener(this);  
  15.     }  
  16.   
  17.     @Override  
  18.     public void onClick(View v) {  
  19.         if(v.getId() == R.id.rl_more){  
  20.             PopWindow popWindow = new PopWindow(this);  
  21.             popWindow.showPopupWindow(findViewById(R.id.rl_more));  
  22.         }  
  23.     }  
  24. }  

效果圖:    
發佈了107 篇原創文章 · 獲贊 187 · 訪問量 114萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章