Custom PopupWindow and popupwindow

Source: Internet
Author: User

Custom PopupWindow and popupwindow

 

PopupWindow: a pop-up window control that can be used to display any View and floating at the top of the current activity

Custom PopupWindow.

1. extends PopupWindow

2. Some attribute settings can be performed in the constructor.

 

SetContentView (View convertView); // set the View displayed in popupWindow

GetContentView (); // obtain the view displayed in popupWindow

SetWidth (mWith); // set the width of popupWindow

SetHeight (mHeight); // set the height of popupWindow

You can also specify the width and height in the constructor. In addition to writing specific values, you can also use WRAP_CONTENT or MATCH_PARENT, the width and height attributes of popupWindow correspond directly to the first View.

SetAnimationStyle (R. style. PopupAnimation); // you can specify the animation.

 

SetFocusable (true );

Set focus. After PopupWindow pops up, all touch-screen and physical buttons are processed by PopupWindows. The response to any other event must occur after the PopupWindow disappears (except for system-level events such as home ). For example, when such a PopupWindow appears, press the back key to first make the PopupWindow disappear, and press the second button to exit the activity. To be precise, to exit the activity, you must first make the PopupWindow disappear, because it does not disappear when you press back PopupWindow under any circumstances, the background must be set in PopupWindow.

SetFocusable (false );//PopUpWindow is just a view that appears on the current interface and does not affect any operations on the current interface. It is a non-existent thing.

Generally, setFocusable (true );

SetTouchable (true ); // Set popupwindow and click

To make PopupWindow disappear outside of PopupWindow:

1. setOutsideTouchable (true );

2. Call setBackgroundDrawable (new BitmapDrawable (); set the background. In order not to affect the style, the background is empty.

In addition, setBackgroundDrawable (new ColorDrawable (0x00000000) can be written in this way. The background is not empty but completely transparent.

After the actual test, as long as the background is set, whether or not setOutsideTouchable (true) is set; even if setOutsideTouchable (false) is set, PopupWindow disappears outside the PopupWindow, which is a little strange, for the sake of insurance, it is better to add setOutsideTouchable (true.

 

PopupWindow also has a method that is used to set and click the outside of the PopupWindow to make the PopupWindow disappear. However, this method alone does not work. It also takes effect when setting the background. However, when the background is set, whether or not this method is used or not, you can click the outside of PopupWindow to make the PopupWindow disappear, which is too drunk.

setTouchInterceptor(new OnTouchListener()         {            @Override            public boolean onTouch(View v, MotionEvent event)             {                if(event.getAction()==MotionEvent.ACTION_OUTSIDE)                {                    dismiss();                    return true;                }                return false;            }        });

 

 

Set the pop-up position of PopupWindow

PopupWindow = new SelectPicPopupWindow (parameter );

PopupWindow. showAtLocation (findViewById (R. id. settings_layout ),
Gravity. BOTTOM | Gravity. CENTER_HORIZONTAL, 0, 0 );

 // Here findViewById (R. id. settings_layout) is the id of the current interface.

 

Set the background of PopupWindow, click the Back key, or click the external area of the pop-up window. The pop-up window will dismiss.On the contrary, if you do not set the background of PopupWindow, click back and click the external area of the pop-up window, And the pop-up window will not disappear.

If I want an effect, click the external area and the pop-up window will not disappear, but the click event will be passed down to the activity. For example, the following is a WebView, And I want to click the link in it.

/*** Click the external area and the Window will not disappear. However, the click event will be passed down to the activity. You need to set a Flag for the Window, * WindowManager. layoutParams. FLAG_NOT_TOUCH_MODAL * reads the source code. Whether the Flag is set is controlled by a field called mNotTouchModal, but the set method for setting this field is marked as @ hide. * You need to call this method through reflection: * Set whether this window is touch modal or if outside touches will be sent * to * other windows behind it. **/public static void setPopupWindowTouchModal (PopupWindow popupWindow, boolean touchModal) {if (null = popupWindow) {return;} Method method; try {method = PopupWindow. class. getDeclaredMethod ("setTouchModal", boolean. class); method. setAccessible (true); method. invoke (popupWindow, touchModal);} catch (Exception e) {e. printStackTrace ();}}

 

Call setPopupWindowTouchModal (popupWindow, false.

 

 However, note the following:

1. setHeight () is set. If the set height is smaller than the screen height, the transparent part does not belong to the PopupWindow.

2. set setContentView (View v). Because the components in v are not fully occupied by the entire Screen in the layout file, for example, they are only placed at the bottom, making the pop-up PopupWindow partially transparent, this transparency part still belongs to the PopupWindow. What should I do if I want to click the transparency part to make the PopupWindow disappear?

This is not difficult. The answer is as follows:

/**  *public class SelectPicPopupWindow extends PopupWindow  */private View popupView;popupView = inflater.inflate(R.layout.popup_window, null);this.setContentView(popupView);popupView.setOnTouchListener(new OnTouchListener() {            @SuppressLint("ClickableViewAccessibility")            @Override            public boolean onTouch(View v, MotionEvent event) {                int height = popupView.findViewById(R.id.popup_layout).getHeight();                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.