There are two types of Android dialogs: PopupWindow and AlertDialog. Their differences are:
The position of AlertDialog is fixed, while the position of PopupWindow can be random.
AlertDialog is a non-blocking thread, while PopupWindow is a blocking thread.
The position of PopupWindow can be divided into two types: Offset and no offset based on whether there is any offset. According to the difference of the reference object, it can be divided into a control (Anchor) and a parent control. The details are as follows:
ShowAsDropDown (View anchor): relative to a control (lower left), no offset
ShowAsDropDown (View anchor, int xoff, int yoff): The position of a widget with an offset.
ShowAtLocation (View parent, int gravity, int x, int y): The position relative to the parent control (for example, central Gravity. CENTER. Gravity. BOTTOM, etc.), you can set an offset or no offset
The following is a Demo (for more information, see the Notes ):
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 = "Use your own as Anchor, without offset"/>
<Button
Android: id = "@ + id/button02"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Use your own Anchor, with an offset"/>
<Button
Android: id = "@ + id/button03"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "take the center of the screen as the reference, no offset (median)"/>
<Button
Android: id = "@ + id/button04"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "in the middle of the screen as a reference"/>
</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;
// Screen width
Private int mScreenWidth;
// Screen height
Private int mScreenHeight;
// PopupWindow width
Private int mpopup1_wwidth;
// Height of 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 a widget (bottom left), no offset
Case R. id. button01:
GetPopupWindowInstance ();
MPopupWindow. showAsDropDown (v );
Break;
// Relative to a widget (bottom left), with an offset
Case R. id. button02:
GetPopupWindowInstance ();
MPopupWindow. showAsDropDown (v, 50, 50); // The offset of each of the X and Y directions is 50.
Break;
// No offset relative to the parent widget
Case R. id. button03:
GetPopupWindowInstance ();
MPopupWindow. showAtLocation (v, Gravity. CENTER, 0, 0 );
Break;
// There is an 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 ();
}
/*
* Obtain a PopupWindow instance
*/
Private void getpopup1_winstance (){
If (null! = MPopupWindow ){
MPopupWindow. dismiss ();
Return;
} Else {
InitPopuptWindow ();
}
}
/*
* Create a 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 specifies the content of PopupWindow
// Parameter 2: width specifies the width of the PopupWindow
// Parameter 3: height specifies the height of PopupWindow
MPopupWindow = new PopupWindow (popupWindow, 100,130 );
// Obtain the width and height of the screen and PopupWindow.
MScreenWidth = getWindowManager (). getdefadisplay display (). getWidth ();
MScreenWidth = getWindowManager (). getdefadisplay display (). getHeight ();
Mpopup1_wwidth = mPopupWindow. getWidth ();
MPopupWindowHeight = mPopupWindow. getHeight ();
}
}
From the column of zookeeper