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. }  

效果图:      
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值