Android uses the Popupwindow implementation page to click on the top pop-up pull-down menu _android

Source: Internet
Author: User

Implementation of this feature does not have too many technical difficulties, mainly through the Popupwindow method, at the same time further deepen the use of Popupwindow, realize click Pop-up a custom View,view inside can be free to design, more commonly used to put a ListView.

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

Nonsense not to say, directly on the code:

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 () {////Get pop.xml View CustomView = Getlayoutinflater () for custom layout files. I 
    Nflate (R.layout.popview_item, NULL, FALSE); Create Popupwindow instance, 200,150 is width and height Popupwindow = new Popupwindow (cusTomview, 250, 280); 
    Set the animation effect [R.style.animationfade is its own predefined] Popupwindow.setanimationstyle (R.style.animationfade); Custom View Add Touch event Customview.setontouchlistener (new Ontouchlistener () {@Override public boolean Ontouch (View V, motionevent event) 
          {if (Popupwindow!= null && popupwindow.isshowing ()) {Popupwindow.dismiss (); 
        Popupwindow = null; 
      return false; 
    } 
    }); 
    /** Here you can implement a custom view function/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= " Click on the dropdown list "/> 

Customizing view's 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:backg" round= "#C0C0C0" > <button android:id= "@+id/button2" android:layout_width= "200DP" android:layout_he ight= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" Android:padd ingright= "70DP" android:text= "Viviens"/> <button android:id= "@+id/button3" 200DP "android:layout_height=" Wrap_content "android:layout_alignparentleft=" true "android:layout_below=" @+i 
    D/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= "Zhang Xiaoda"/> </RelativeLayout>  

Animation effect:

Inputodown.xml into the screen

<?xml version= "1.0" encoding= "UTF-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate 
    android:duration=" 
    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=" 
    android:fromydelta= "0" 
    android:toydelta= " -100%"/> 
</set>

Styles.xml

<style name= "Animationfade" > 
  <!--popupwindow pop-up effect--> <item 
  "Android: Windowenteranimation "> @anim/inuptodown</item> 
  <item name=" Android:windowexitanimation "> @anim /outdowntoup</item> 

Implementation effect:


The above is the article on Android using the Popupwindow implementation page to click on the top pop-up drop-down menu of all the content, I hope you like.

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.