【android開發】使用PopupWindow實現頁面點擊頂部彈出下拉式功能表

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   使用   strong   io   

 

沒有太多花樣,也沒有很複雜的技術,就是簡單的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, 0, 5);  
  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.                 null, false);  
  40.         // 建立PopupWindow執行個體,200,150分別是寬度和高度  
  41.         popupwindow = new PopupWindow(customView, 250, 280);  
  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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.