Android Popupwindow implement right, left, and bottom pop-up menus _android

Source: Internet
Author: User
Tags set background

This tutorial for you to share the Android Popupwindow pop-up menu specific code, for your reference, the specific content as follows

Project code: Http://xiazai.jb51.net/201611/yuanma/PopupLeftMenu (jb51.net). rar

The Project SDK is 5.1, and it is recommended that you copy the code to your own project.

The code is as follows:

Mainactivity class:

Package com.example.popupleftmenu; 
Import android.app.Activity; 
Import Android.content.Context; 
Import android.graphics.drawable.ColorDrawable; 
Import Android.os.Bundle; 
Import android.view.Gravity; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.view.View.OnTouchListener; 
Import Android.view.ViewGroup.LayoutParams; 
Import Android.view.WindowManager; 
Import Android.widget.Button; 
Import Android.widget.PopupWindow; 
 
Import Android.widget.Toast; 
 public class Mainactivity extends activity {private context = null; 
 Private Popupwindow Popupwindow; 
  
 private int from = 0; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  context = this; 
  Setcontentview (R.layout.activity_main); 
  Button popleftbtn = (button) Findviewbyid (R.ID.POP_LEFT_BTN); 
  Button poprightbtn = (button) Findviewbyid (R.ID.POP_RIGHT_BTN); Button popbottombtn = (Button) Findviewbyid (R.ID.POP_BOTTOM_BTN); 
  Popleftbtn.setonclicklistener (Popclick); 
  Poprightbtn.setonclicklistener (Popclick); 
 Popbottombtn.setonclicklistener (Popclick); } onclicklistener Popclick = new Onclicklistener () {@Override public void OnClick (View v) {switch (v 
     . GetId ()) {case r.id.pop_left_btn:{from = Location.LEFT.ordinal (); 
    Break 
     Case r.id.pop_right_btn:{from = Location.RIGHT.ordinal (); 
    Break 
     Case r.id.pop_bottom_btn:{from = Location.BOTTOM.ordinal (); 
    Break 
   }//Call this method, menu does not top//popupwindow.showasdropdown (v); 
    
  Initpopupwindow (); 
 } 
 }; /** * The Popwin Shutdown event that pops up when new notes are added is mainly to change the background transparency * * */class Popupdismisslistener implements Popupwindow.ondismissliste 
  ner{@Override public void Ondismiss () {Backgroundalpha (1f); } protected void Initpopupwindow () {View Popupwindowview = Getlayoutinflater (). Inflate (R.Layout.pop, NULL); Content, Height, width if (Location.BOTTOM.ordinal () = from) {Popupwindow = new Popupwindow (Popupwindowview, Layoutparams.fill_ 
  PARENT, Layoutparams.wrap_content, true); 
  }else{Popupwindow = new Popupwindow (Popupwindowview, Layoutparams.wrap_content, layoutparams.fill_parent, true); 
  }//Animation effect if (Location.LEFT.ordinal () = from) {Popupwindow.setanimationstyle (R.style.animationleftfade); 
  }else if (Location.RIGHT.ordinal () = = from) {Popupwindow.setanimationstyle (R.style.animationrightfade); 
  }else if (Location.BOTTOM.ordinal () = = from) {Popupwindow.setanimationstyle (R.style.animationbottomfade); 
  }//Menu background color colordrawable DW = new colordrawable (0xFFFFFFFF); 
  Popupwindow.setbackgrounddrawable (DW); 
  Width//popupwindow.setwidth (layoutparams.wrap_content); 
  Height//popupwindow.setheight (layoutparams.fill_parent); Display position if (Location.LEFT.ordinal () = = from) {popupwindow.showatlocation (Getlayoutinflater (). Inflate (r.layout.aCtivity_main, NULL), Gravity.left, 0, 500); }else if (Location.RIGHT.ordinal () = = from) {popupwindow.showatlocation (Getlayoutinflater (). Inflate ( 
  R.layout.activity_main, NULL), Gravity.right, 0, 500); }else if (Location.BOTTOM.ordinal () = = from) {popupwindow.showatlocation (Getlayoutinflater (). Inflate ( R.layout.activity_main, null), gravity.bottom| 
  Gravity.center_horizontal, 0, 0); 
  //Set Background translucent backgroundalpha (0.5f); 
   
  Closes the event Popupwindow.setondismisslistener (New Popupdismisslistener ()); Popupwindowview.setontouchlistener (New Ontouchlistener () {@Override public boolean ontouch (View V, Motioneve 
     NT Event) {/*if (popupwindow!=null && popupwindow.isshowing ()) {Popupwindow.dismiss (); 
    Popupwindow=null; 
   }*///Here if true, the touch event will be intercepted//intercepted after Popupwindow's ontouchevent is not invoked, so clicking on the external area cannot dismiss return false; 
   
  } 
  }); 
  Button open = (Button) Popupwindowview.findviewbyid (R.id.open); Button save = (Button) pOpupwindowview.findviewbyid (R.id.save); 
   
   
  Button close = (Button) Popupwindowview.findviewbyid (r.id.close); Open.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Toast.maketext (c 
    Ontext, "Open", Toast.length_long). Show (); 
   Popupwindow.dismiss (); 
   
  } 
  }); Save.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Toast.maketext (c 
    Ontext, "Open", Toast.length_long). Show (); 
   Popupwindow.dismiss (); 
   
  } 
  }); Close.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Toast.maketext ( 
    Context, "Open", Toast.length_long). Show (); 
   Popupwindow.dismiss (); 
 } 
  }); /** * settings to add screen background transparency * @param bgalpha */public void Backgroundalpha (float bgalpha) {WINDOWMANAGER.L 
  Ayoutparams LP = GetWindow (). GetAttributes (); Lp.alpha = Bgalpha; 
 0.0-1.0 GetWindow (). SetAttributes (LP); 
} 
 /**  * Menu Popup Direction */public enum Location {left, right, top, BOTTOM;  } 
}

Two layout files:

1.activity_main.xml, just three button.

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 xmlns:tools= "http:// Schemas.android.com/tools " 
 android:layout_width=" fill_parent " 
 android:layout_height=" Fill_parent 
 " android:orientation= "vertical" > 
 
 <button 
  android:id= "@+id/pop_left_btn" 
  android:layout_width= "Fill_parent" 
  android:layout_height= "wrap_content" 
  android:text= "@string/pop_left"/> 
  
 < Button 
  android:id= "@+id/pop_right_btn" 
  android:layout_width= "fill_parent" 
  android:layout_height = "Wrap_content" 
  android:text= "@string/pop_right"/> 
  
 <button android:id= 
  "@+id/pop_bottom_ BTN " 
  android:layout_width=" fill_parent " 
  android:layout_height=" wrap_content " 
  android:text=" @ String/pop_bottom "/> 
  
 
</LinearLayout>

2. Pop.xml, also three button, you can modify your own

 <?xml version= "1.0" encoding= "Utf-8"?> "<linearlayout xmlns:android=" http:// 
 Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " android:orientation= "Vertical" > <!--<linearlayout android:layout_width= "Wrap_content" android:layout _height= "fill_parent" android:orientation= "vertical" android:background= "#ffffff" >--> <button Andro Id:id= "@+id/open" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@strin G/open "/> <button android:id=" @+id/save "android:layout_width=" fill_parent "android:layout_height=" WR Ap_content "android:text=" @string/save "/> <button android:id=" @+id/close "android:layout_width=" Fill_  Parent "android:layout_height=" wrap_content "android:text=" @string/close "/> <!--</LinearLayout> --> </LinearLayout> 

Strings.xml

<string name= "Pop_left" > Pop-up left menu </string> 
 <string name= "Pop_right" > Popup Right Menu </string> 
 <string name= "Pop_bottom" > Pop-up bottom menu </string> 
 <string name= "open" > Open </string> 
 <string name= "Save" > Save </string> 
 <string name= "Close" > Off </string> 

Styles.xml

<style name= "Animationleftfade" > 
  <item name= "android:windowenteranimation" > @anim/in_lefttoright </item> 
  <item name= "android:windowexitanimation" > @anim/out_righttoleft</item> 
 </ style> 
  
 <style name= "Animationrightfade" > 
  <item name= "Android:windowenteranimation" > @anim in_righttoleft</item> 
  <item name= "android:windowexitanimation" > @anim/out_lefttoright</item > 
 </style> 
  
 <style name= "Animationbottomfade" > 
  <item name= "Android: Windowenteranimation "> @anim/in_bottomtotop</item> 
  <item name=" Android:windowexitanimation "> @anim/out_toptobottom</item> 
 </style> 

Left pop-up menu animation file:

In_lefttoright.xml: From left into

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  
 <translate 
  android:fromxdelta=" -100% " 
  android:toxdelta=" 0 " 
  android:duration=" "/> 
  
</set> 

Out_righttoleft.xml: From the right

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

Other animated files are written for their own reference, which is used by Fromxdelta, Fromydelta, Toxdelta and Toydelta.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.