"Android Development" using Popupwindow implementation page Click on the top pop-up drop-down menu

Source: Internet
Author: User

Not too many tricks, there is no very complex technology, is the use of simple popupwindow, you can achieve click to pop a custom View,view can be casually designed, commonly used can put a ListView.


Demo I just a click to show, simple use of the fade in the animation effect, there is no exquisite picture resources, look also ugly, but such a short time, let you master a very useful technology, you can expand, not very good?



Nonsense not to say, directly on the code:

Mainactivity.java

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity implements Onclicklistener {
  2. private Popupwindow Popupwindow;
  3. Private button button;
  4. @Override
  5. protected void OnCreate (Bundle savedinstancestate) {
  6. super.oncreate (savedinstancestate);
  7. Setcontentview (R.layout.activity_main);
  8. Button = (button) Findviewbyid (R.id.button1);
  9. Button.setonclicklistener (this);
  10. }
  11. @Override
  12. public void OnClick (View v) {
  13. switch (V.getid ()) {
  14. Case R.id.button1:
  15. if (Popupwindow! = null&&popupwindow.isshowing ()) {
  16. Popupwindow.dismiss ();
  17. return;
  18. } Else {
  19. Initmpopupwindowview ();
  20. Popupwindow.showasdropdown (V, 0, 5);
  21. }
  22. Break ;
  23. Default:
  24. Break ;
  25. }
  26. }
  27. public void Initmpopupwindowview () {
  28. //// Get a view of the custom layout file Pop.xml
  29. View CustomView = Getlayoutinflater (). Inflate (R.layout.popview_item,
  30. null, false);
  31. //Create Popupwindow instances, 200,150 are width and height, respectively
  32. Popupwindow = New Popupwindow (CustomView, 280);
  33. //Set animation effect [R.style.animationfade is defined by oneself beforehand]
  34. Popupwindow.setanimationstyle (R.style.animationfade);
  35. //Custom View Add Touch event
  36. Customview.setontouchlistener (new Ontouchlistener () {
  37. @Override
  38. Public Boolean OnTouch (View V, motionevent event) {
  39. if (Popupwindow! = null && popupwindow.isshowing ()) {
  40. Popupwindow.dismiss ();
  41. Popupwindow = null;
  42. }
  43. return false;
  44. }
  45. });
  46. /** Here you can implement the function of the custom view * /
  47. Button Btton2 = (button) Customview.findviewbyid (R.id.button2);
  48. Button Btton3 = (button) Customview.findviewbyid (R.id.button3);
  49. Button Btton4 = (button) Customview.findviewbyid (R.ID.BUTTON4);
  50. Btton2.setonclicklistener (this);
  51. Btton3.setonclicklistener (this);
  52. Btton4.setonclicklistener (this);
  53. }
  54. }


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. <button
  8. android:id="@+id/button1"
  9. Android:layout_width="Match_parent"
  10. android:layout_height="Wrap_content"
  11. android:layout_alignparentleft="true"
  12. android:layout_alignparenttop="true"
  13. android:gravity="center"
  14. android:background="#C0C0C0"
  15. android:text="Click Drop-down list"/>
  16. </RelativeLayout>


XML for Custom View

[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. <button
  7. android:id="@+id/button2"
  8. Android:layout_width="200DP"
  9. android:layout_height="Wrap_content"
  10. android:layout_alignparentleft="true"
  11. android:layout_alignparenttop="true"
  12. android:paddingright="70DP"
  13. android:text="Viviens"/>
  14. <button
  15. android:id="@+id/button3"
  16. Android:layout_width="200DP"
  17. android:layout_height="Wrap_content"
  18. android:layout_alignparentleft="true"
  19. android:layout_below="@+id/button2"
  20. android:paddingright="70DP"
  21. android:text="Mryang"/>
  22. <button
  23. android:id="@+id/button4"
  24. Android:layout_width="200DP"
  25. android:layout_height="Wrap_content"
  26. android:layout_alignparentleft="true"
  27. android:layout_below="@+id/button3"
  28. android:paddingright="70DP"
  29. android:text="Zhang Xiaoda"/>
  30. </RelativeLayout>


Animation effect:

Inputodown.xml Entering the screen

[Java]View Plaincopy
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
    3. <translate
    4. android:duration="
    5. Android:fromydelta=" -100%"
    6. Android:toydelta="0"/>
    7. </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. <translate
    4. android:duration="
    5. Android:fromydelta="0"
    6. Android:toydelta=" -100%"/>
    7. </set>


Styles.xml

[HTML]View Plaincopy
  1. <style name="Animationfade">
  2. <!--Popupwindow effect and left
  3. <item name="android:windowenteranimation"> @anim/inuptodown</Item >
  4. <item name="android:windowexitanimation"> @anim/outdowntoup</Item>
  5. </style>




Implementation results:



Demo Address:

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.