Android: Use Cases and precautions of PopupWindow, androidpopupwindow

Source: Internet
Author: User

Android: Use Cases and precautions of PopupWindow, androidpopupwindow

1. Features of PopupWindow

According to the official Google statement:

"A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity."

That is to say, popupwindow is activityTopIt can display any View, which is very domineering. Next, let's take a look at how it works.

2. initialize some features of PopupWindow

Example:

PopupWindow popupWindow = new PopupWindow (getApplicationContext (); popupWindow. setContentView (contentView); // you can set any ViewpopupWindow. setWidth (LayoutParams. WRAP_CONTENT); // set the width of popupWindow. setHeight (LayoutParams. WRAP_CONTENT); // height popupWindow. setAnimationStyle (R. anim. abc_fade_in); // displays the animation popupWindow. setFocusable (true); // sets whether to obtain the focus.

Here, contentView is the View you want to display.

3. Display and hide PopupWindow

Display Method:

public void showAtLocation (View parent, int gravity, int x, int y)Added in API level 1Display the content view in a popup window at the specified location. If the popup window cannot fit on screen, it will be clipped. See WindowManager.LayoutParams for more information on how gravity and the x and y parameters are related. Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP.Parametersparenta parent view to get the getWindowToken() token fromgravitythe gravity which controls the placement of the popup windowxthe popup's x location offsetythe popup's y location offset

PopupWindow. showAtLocation (contentView, Gravity. CENTER, 0, 0); // set CENTER

PopupWindow. showAtLocation (contentView, Gravity. NO_GRAVITY, x, y); // you can specify (x, y) as the position in the upper left corner of the window.


Hide:

if (popupWindow != null&& popupWindow.isShowing()) {popupWindow.dismiss();popupWindow = null;}


Note: When calculating the view position:

Android provides some methods to obtain the position of a View on the screen.
1). getLocationOnScreen: Calculate the x and y values of the view in the global coordinate system, and obtain the absolute coordinates in the current screen (the value starts from the top of the screen, including the notice Bar Height ).
2). getLocationInWindow: calculates the x and y coordinates of the view in its widnow.
3) getLeft, getTop, getBottom, and getRight are the coordinates in the parent layout.


Related: popupwindow Animation: http://blog.csdn.net/wl455624651/article/details/7798879

For more information, the Android Development Alliance's QQ group: 272209595




How can I use the Imagebutton response in the pop-up form of popupwindow (android programming )??

Try writing the response outside.

That is, when you create a popupwindow, make a view = popupwindow
Then ImageButton x = (ImageButton) view. findViewById (xxxxxx)
X. setOnclickListenner (xxx ){
New OnclickListener {
Xxx
}
}
(If xml is not loaded, add the event before ImageButton is added)
Then all the variables used in OnclickListenr are either defined as class variables or final variables.
Are you trying

In android, how does one listen to the home package of popupwindow?

Unable to listen.
Bytes -----------------------------------------------------------------------------------------------------
Answers from the android team
Welcome to the android excellent team

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.