Use PopupWindow to slide the selection menu from the bottom

Source: Internet
Author: User

In development, the pop-up effect of PopupWindow is indispensable. Many apps use PopupWindow. Such as Sina Weibo client, client, and public comment client. Today, we will introduce a menu slide from the bottom using PopupWindow in the client. In this example, the pop-up window inherits the pop-up form implemented by the PopupWindow class. The layout can be customized. The pop-up effect is mainly implemented using the translate and alpha styles. The example is as follows: Step 1: Design the pop-up window xml: [java] <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center_horizontal" android: orientation = "vertical"> <LinearLayout android: id = "@ + id/pop_layout" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center_horizontal" android: orientation = "vertical" android: layout_alignParentBottom = "true" android: background = "@ drawable/strong"> <Button android: id = "@ + id/btn_take_photo" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: layout_marginTop = "20dip" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "" android: background = "@ drawable/btn_style_alert_dialog_button" android: textStyle = "bold"/> <Button android: id = "@ + id/btn_pick_photo" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: layout_marginTop = "5dip" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "select" android: background = "@ drawable/btn_style_alert_dialog_button" android: textStyle = "bold"/> <Button android: id = "@ + id/btn_cancel" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: layout_marginTop = "15dip" android: layout_marginBottom = "15dip" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "cancel" android: background = "@ drawable/btn_style_alert_dialog_cancel" android: textColor = "# ffffff" android: textStyle = "bold"/> </LinearLayout> </RelativeLayout> Step 2: Create the SelectPicPopupWindow class to inherit from the PopupWindow: [java] import android. app. activity; import android. content. context; import android. graphics. drawable. colorDrawable; import android. view. layoutInflater; 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. widget. button; import android. widget. popupWindow; public class SelectPicPopupWindow extends PopupWindow {private Button values, values, btn_cancel; private View mMenuView; public SelectPicPopupWindow (Activity context, OnClickListener itemsOnClick) {super (context ); layoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); mMenuView = inflater. inflate (R. layout. alert_dialog, null); btn_take_photo = (Button) mMenuView. findViewById (R. id. btn_take_photo); btn_pick_photo = (Button) mMenuView. findViewById (R. id. btn_pick_photo); btn_cancel = (Button) mMenuView. findViewById (R. id. btn_cancel); // cancel the button btn_cancel.setOnClickListener (new OnClickListener () {public void onClick (View v) {// destroy the pop-up box dismiss ();}}); // set the button to listen to btn_pick_photo.setOnClickListener (itemsOnClick); btn_take_photo.setOnClickListener (itemsOnClick); // set the View of SelectPicPopupWindow this. setContentView (mMenuView); // you can specify the width of the SelectPicPopupWindow window. setWidth (LayoutParams. FILL_PARENT); // set the height of the SelectPicPopupWindow pop-up form this. setHeight (LayoutParams. WRAP_CONTENT); // you can click this to set the SelectPicPopupWindow pop-up window. setFocusable (true); // set SelectPicPopupWindow to bring up the form animation effect this. setAnimationStyle (R. style. animBottom); // instantiate a ColorDrawable color as translucent ColorDrawable dw = new ColorDrawable (0xb0000000); // set the background of the SelectPicPopupWindow pop-up form this. setBackgroundDrawable (dw); // added the OnTouchListener listener to the mMenuView to determine whether to obtain the touch screen position. If it is outside the selection box, the mMenuView pop-up box is destroyed. setOnTouchListener (new OnTouchListener () {public boolean onTouch (View v, MotionEvent event) {int height = mMenuView. findViewById (R. id. pop_layout ). getTop (); int y = (int) event. getY (); if (event. getAction () = MotionEvent. ACTION_UP) {if (y

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.