PopupWindow 顯示下拉菜單

沒有太多花樣,也沒有很複雜的技術,就是簡單的PopupWindow的使用,可以實現點擊彈出一個自定義的view,view裏可以隨便設計,常用的可以放一個listview。


demo中我只是一個點擊展示,簡單的使用了fade in out的動畫效果,也沒有精美的圖片資源,看着也醜,不過這麼短的時間,讓你掌握一個很好用的技術,可以自己擴展,不很好麼?



廢話不說了,直接上代碼:

MainActivity.java

[java] view plaincopy
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.   
  3.     private PopupWindow popupwindow;  
  4.     private Button button;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         button = (Button) findViewById(R.id.button1);  
  12.         button.setOnClickListener(this);  
  13.   
  14.     }  
  15.   
  16.     @Override  
  17.     public void onClick(View v) {  
  18.   
  19.         switch (v.getId()) {  
  20.         case R.id.button1:  
  21.             if (popupwindow != null&&popupwindow.isShowing()) {  
  22.                 popupwindow.dismiss();  
  23.                 return;  
  24.             } else {  
  25.                 initmPopupWindowView();  
  26.                 popupwindow.showAsDropDown(v, 05);  
  27.             }  
  28.             break;  
  29.         default:  
  30.             break;  
  31.         }  
  32.   
  33.     }  
  34.   
  35.     public void initmPopupWindowView() {  
  36.   
  37.         // // 獲取自定義佈局文件pop.xml的視圖  
  38.         View customView = getLayoutInflater().inflate(R.layout.popview_item,  
  39.                 nullfalse);  
  40.         // 創建PopupWindow實例,200,150分別是寬度和高度  
  41.         popupwindow = new PopupWindow(customView, 250280);  
  42.         // 設置動畫效果 [R.style.AnimationFade 是自己事先定義好的]  
  43.         popupwindow.setAnimationStyle(R.style.AnimationFade);  
  44.         // 自定義view添加觸摸事件  
  45.         customView.setOnTouchListener(new OnTouchListener() {  
  46.   
  47.             @Override  
  48.             public boolean onTouch(View v, MotionEvent event) {  
  49.                 if (popupwindow != null && popupwindow.isShowing()) {  
  50.                     popupwindow.dismiss();  
  51.                     popupwindow = null;  
  52.                 }  
  53.   
  54.                 return false;  
  55.             }  
  56.         });  
  57.   
  58.         /** 在這裏可以實現自定義視圖的功能 */  
  59.         Button btton2 = (Button) customView.findViewById(R.id.button2);  
  60.         Button btton3 = (Button) customView.findViewById(R.id.button3);  
  61.         Button btton4 = (Button) customView.findViewById(R.id.button4);  
  62.         btton2.setOnClickListener(this);  
  63.         btton3.setOnClickListener(this);  
  64.         btton4.setOnClickListener(this);  
  65.   
  66.     }  
  67.   
  68. }  


activity_main.xml

[java] view plaincopy
  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.     android:background="#000000"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:gravity="center"  
  15.         android:background="#C0C0C0"  
  16.         android:text="點擊下拉列表" />  
  17.   
  18. </RelativeLayout>  


自定義view的xml

[java] view plaincopy
  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.     android:background="#C0C0C0" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button2"  
  9.         android:layout_width="200dp"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:paddingRight="70dp"  
  14.         android:text="viviens" />  
  15.   
  16.     <Button  
  17.         android:id="@+id/button3"  
  18.         android:layout_width="200dp"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentLeft="true"  
  21.         android:layout_below="@+id/button2"  
  22.         android:paddingRight="70dp"  
  23.         android:text="mryang" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/button4"  
  27.         android:layout_width="200dp"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignParentLeft="true"  
  30.         android:layout_below="@+id/button3"  
  31.         android:paddingRight="70dp"  
  32.         android:text="張曉達" />  
  33.   
  34. </RelativeLayout>  


動畫效果:

inputodown.xml 進入屏幕

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="500"  
  6.         android:fromYDelta="-100%"  
  7.         android:toYDelta="0" />  
  8.   
  9. </set>  

outdowntoup.xml

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="500"  
  6.         android:fromYDelta="0"  
  7.         android:toYDelta="-100%" />  
  8.   
  9. </set>  


styles.xml

[html] view plaincopy
  1. <style name="AnimationFade">  
  2.   
  3.     <!-- PopupWindow左右彈出的效果 -->  
  4.     <item name="android:windowEnterAnimation">@anim/inuptodown</item>  
  5.     <item name="android:windowExitAnimation">@anim/outdowntoup</item>  
  6. </style>  




實現效果:



demo地址:

http://download.csdn.net/detail/mad1989/5518035


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章