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

來源:互聯網
上載者:User

實現此功能沒有太多的技術痛點,主要通過PopupWindow方法,同時更進一步加深了PopupWindow的使用,實現點擊彈出一個自訂的view,view裡面可以自由設計,比較常用的可以放一個listview。

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

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

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {   private PopupWindow popupwindow;   private Button button;   @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     button = (Button) findViewById(R.id.button1);     button.setOnClickListener(this);   }   @Override   public void onClick(View v) {     switch (v.getId()) {     case R.id.button1:       if (popupwindow != null&&popupwindow.isShowing()) {         popupwindow.dismiss();         return;       } else {         initmPopupWindowView();         popupwindow.showAsDropDown(v, 0, 5);       }       break;     default:       break;     }   }   public void initmPopupWindowView() {     // // 擷取自訂布局檔案pop.xml的視圖     View customView = getLayoutInflater().inflate(R.layout.popview_item,         null, false);     // 建立PopupWindow執行個體,200,150分別是寬度和高度     popupwindow = new PopupWindow(customView, 250, 280);     // 設定動畫效果 [R.style.AnimationFade 是自己事先定義好的]     popupwindow.setAnimationStyle(R.style.AnimationFade);     // 自訂view添加觸摸事件     customView.setOnTouchListener(new OnTouchListener() {       @Override       public boolean onTouch(View v, MotionEvent event) {         if (popupwindow != null && popupwindow.isShowing()) {           popupwindow.dismiss();           popupwindow = null;         }         return false;       }     });     /** 在這裡可以實現自訂視圖的功能 */     Button btton2 = (Button) customView.findViewById(R.id.button2);     Button btton3 = (Button) customView.findViewById(R.id.button3);     Button btton4 = (Button) customView.findViewById(R.id.button4);     btton2.setOnClickListener(this);     btton3.setOnClickListener(this);     btton4.setOnClickListener(this);   } } 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:background="#000000"   tools:context=".MainActivity" >   <Button     android:id="@+id/button1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_alignParentTop="true"     android:gravity="center"     android:background="#C0C0C0"     android:text="點擊下拉式清單" /> </RelativeLayout> 

自訂view的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:background="#C0C0C0" >   <Button     android:id="@+id/button2"     android:layout_width="200dp"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_alignParentTop="true"     android:paddingRight="70dp"     android:text="viviens" />   <Button     android:id="@+id/button3"     android:layout_width="200dp"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_below="@+id/button2"     android:paddingRight="70dp"     android:text="mryang" />   <Button     android:id="@+id/button4"     android:layout_width="200dp"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_below="@+id/button3"     android:paddingRight="70dp"     android:text="張曉達" /> </RelativeLayout> 

動畫效果:

inputodown.xml 進入螢幕

<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >   <translate     android:duration="500"     android:fromYDelta="-100%"     android:toYDelta="0" /> </set>

outdowntoup.xml

<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >   <translate     android:duration="500"     android:fromYDelta="0"     android:toYDelta="-100%" /> </set>

styles.xml

<style name="AnimationFade">   <!-- PopupWindow左右彈出的效果 -->   <item name="android:windowEnterAnimation">@anim/inuptodown</item>   <item name="android:windowExitAnimation">@anim/outdowntoup</item> </style> 

實現效果:


以上所述就是本文對android使用PopupWindow實現頁面點擊頂部彈出下拉式功能表的全部內容,希望大家喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.