There are two types of dialog boxes for Android: Popupwindow and Alertdialog. They differ in the following points:
The position of the alertdialog is fixed, while the Popupwindow position can be arbitrarily
Alertdialog is a non-blocking thread, while Popupwindow is a blocking thread
The position of the Popupwindow can be divided into offset and non-offset according to the deviation, and can be divided into relative to a control (anchor anchor) and relative to the parent control depending on the reference. Specific as follows
Showasdropdown (View anchor): relative to the position of a control (just below the left), no offset
Showasdropdown (View anchor, int xoff, int yoff): Offset relative to the position of a control
Showatlocation (View parent, int gravity, int x, int y): can be set offset or no offset relative to the parent control's position (for example, central Gravity.center, Lower gravity.bottom, etc.)
Here is a demo (explanation of the note):
Main.xml
[HTML] <?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" >
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"/>
<button
Android:id= "@+id/button01"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "self-anchor, not offset"/>
<button
Android:id= "@+id/button02"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "with self as anchor, have offset"/>
<button
Android:id= "@+id/button03"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "in the center of the screen as a reference, do not offset (positive middle)"/>
<button
Android:id= "@+id/button04"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "to the bottom of the screen as a reference, bottom middle"/>
</LinearLayout>
Popup_window.xml
[HTML]
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:background= "#00FF00"
android:orientation= "Vertical" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Select Status:"
Android:textcolor= "@android: Color/white"
Android:textsize= "20px"/>
<radiogroup
Android:id= "@+id/radiogroup"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >
<radiobutton android:text= "Online"/>
<radiobutton android:text= "Offline"/>
<radiobutton android:text= "Stealth"/>
</RadioGroup>
</LinearLayout>
Popupwindowdemoactivity.java
[Java]
Package COM.TIANJF;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.PopupWindow;
Import Android.widget.RadioGroup;
Import Android.widget.RadioGroup.OnCheckedChangeListener;
public class Popupwindowdemoactivity extends Activity implements Onclicklistener,
Oncheckedchangelistener {
Private Button Mbutton01;
Private Button mbutton02;
Private Button mbutton03;
Private Button mbutton04;
Private Popupwindow Mpopupwindow;
Width of the screen
private int mscreenwidth;
Height of the screen
private int mscreenheight;
Width of the Popupwindow
private int mpopupwindowwidth;
The height of the Popupwindow
private int mpopupwindowheight;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mbutton01 = (Button) Findviewbyid (R.ID.BUTTON01);
MBUTTON02 = (Button) Findviewbyid (R.ID.BUTTON02);
Mbutton03 = (Button) Findviewbyid (R.ID.BUTTON03);
Mbutton04 = (Button) Findviewbyid (r.id.button04);
Mbutton01.setonclicklistener (this);
Mbutton02.setonclicklistener (this);
Mbutton03.setonclicklistener (this);
Mbutton04.setonclicklistener (this);
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Relative to the position of a control (just below the left), no offset
Case R.ID.BUTTON01:
Getpopupwindowinstance ();
Mpopupwindow.showasdropdown (v);
Break
Relative to the position of a control (just below the left), there is an offset
Case R.ID.BUTTON02:
Getpopupwindowinstance ();
Mpopupwindow.showasdropdown (V, X, y);//Offset 50
Break
No offset relative to the position of the parent control
Case R.ID.BUTTON03:
Getpopupwindowinstance ();
Mpopupwindow.showatlocation (V, gravity.center, 0, 0);
Break
Offset relative to the position of the parent control
Case R.ID.BUTTON04:
Getpopupwindowinstance ();
Mpopupwindow.showatlocation (V, gravity.bottom, 0, 50);
Break
Default
Break
}
}
@Override
public void OnCheckedChanged (radiogroup group, int checkedid) {
Mpopupwindow.dismiss ();
}
/*
* Get Popupwindow instances
*/
private void Getpopupwindowinstance () {
if (null! = Mpopupwindow) {
Mpopupwindow.dismiss ();
Return
} else {
Initpopuptwindow ();
}
}
/*
* Create Popupwindow
*/
private void Initpopuptwindow () {
Layoutinflater Layoutinflater = Layoutinflater.from (this);
View Popupwindow = layoutinflater.inflate (R.layout.popup_window, NULL);
Radiogroup Radiogroup = (radiogroup) Popupwindow.findviewbyid (R.id.radiogroup);
Radiogroup.setoncheckedchangelistener (this);
Create a Popupwindow
Parameter 1:contentview Specify the contents of the Popupwindow
Parameter 2:width Specifies the width of the Popupwindow
Parameter 3:height Specifies the height of the Popupwindow
Mpopupwindow = new Popupwindow (Popupwindow, 100, 130);
Get the width and height of the screen and Popupwindow
Mscreenwidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth ();
Mscreenwidth = Getwindowmanager (). Getdefaultdisplay (). GetHeight ();
Mpopupwindowwidth = Mpopupwindow.getwidth ();
Mpopupwindowheight = Mpopupwindow.getheight ();
}
}
Popupwindow detailed in Android