[Android] uses Popwindow to implement the left-side pop-up menu Layer

Source: Internet
Author: User

PopupWindow can implement the floating layer effect.

The main methods are as follows: You can customize the view. You can use the LayoutInflator method to display an animation when it appears or exits. You can specify the display position.


In order to present multiple functions of PopupWindow and implement them with simple code, a menu function pops up on the left of the click button is compiled, this function automatically disappears when an animation is displayed and exited, and when you click another area.


Activity File
Package com. app. test02; import android. app. activity; import android. OS. bundle; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. widget. button; import android. widget. popupWindow; import android. widget. toast; public class PopwindowLeft extends Activity {// declare the reference of the PopupWindow object private PopupWindow popupWind Ow;/** Called when the activity is first created. * // @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_popupwindow_main); // click the Button to bring up the menu Button pop = (Button) findViewById (R. id. popBtn); pop. setOnClickListener (popClick);} // click OnClickListener popClick = new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubgetPopupWindow (); // The position is displayed in the lower left corner of the button. showAsDropDown (v); // here you can try other effects, such as popupWindow. showAsDropDown (v, // (screenWidth-dialgoWidth)/2, 0); // popupWindow. showAtLocation (findViewById (R. id. layout), // Gravity. CENTER, 0, 0) ;}};/*** create PopupWindow */protected void initPopuptWindow () {// TODO Auto-generated method stub // obtain the View p of the custom Layout file activity_popupwindow_left.xml. OpupWindow_view = getLayoutInflater (). inflate (R. layout. activity_popupwindow_left, null, false); // create a PopupWindow instance. 200,150 is the width and height of popupWindow = new PopupWindow (popupWindow_view, 200,150, true). // set the animation effect popupWindow. setAnimationStyle (R. style. animationFade); // click somewhere else to disappear popupWindow_view.setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-gen Erated method stubif (popupWindow! = Null & popupWindow. isShowing () {popupWindow. dismiss (); popupWindow = null;} return false;}); // The control Button open = (Button) popupwindow_left.xml in the activity_popupwindow_view.findviewbyid (R. id. open); Button save = (Button) popupWindow_view.findViewById (R. id. save); Button close = (Button) popupWindow_view.findViewById (R. id. close); // events triggered by controls in the activity_popupwindow_left.xml view // open. setOnClickListener (New OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // you can perform the Toast operation here. makeText (PopwindowLeft. this, "open operation", 1000); // the dialog box disappears. dismiss () ;}}); // save. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // you can perform the Toast operation here. makeText (PopwindowLeft. this, "save operation", 1000); popupWindow. dismiss ();} }); // Close. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // you can perform the Toast operation here. makeText (PopwindowLeft. this, "close operation", 1000); popupWindow. dismiss () ;}}) ;}/ ***** obtain the PopupWindow instance */private void getPopupWindow () {if (null! = PopupWindow) {popupWindow. dismiss (); return ;}else {initPopuptWindow ();}}}

PopupWindow main configuration file
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: id = "@ + id/popBtn" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "pop-up menu on the left"/> </LinearLayout>

PopupWindow pop-up menu
<? 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: background = "@ android: color/darker_gray" android: orientation = "vertical"> <Button android: id = "@ + id/open" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "@ android: color/darker_gray" android: text = "open"/> <Button android: id = "@ + id/save" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "@ android: color/darker_gray" android: text = "save"/> <Button android: id = "@ + id/close" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "@ android: color/darker_gray" android: text = "close"/> </LinearLayout>

The Animated XML file is displayed in the res folder to create an anim folder. Write the following two files. The animation in_lefttoright.xml is displayed.
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Define the animation from left to right --> <translate android: duration = "500" android: fromXDelta = "-100%" android: toXDelta = "0"/> </set>

Play back the animation out_righttoleft.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Define exit animation from right to left --> <translate android: duration = "500" android: fromXDelta = "0" android: toXDelta = "-100%" type = "codeph" text = "/codeph"/> </set>

In styles. xml, add the following management code.
<Style name = "AnimationFade"> <! -- PopupWindow pop-up results left and right --> <item name = "android: windowEnterAnimation"> @ anim/in_lefttoright </item> <item name = "android: export wexitanimation "> @ anim/out_righttoleft </item> </style>



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.