Android PopupWindow example, androidpopupwindow
The following is an example:
MainActivity. xml
Package sn. qdj. popupwindowdemo; import android. support. v7.app. actionBarActivity; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. popupWindow;/*** use * @ author qingdujun **/public class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle s AvedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/*** popup. xml shows the pop-up interface Layout */View root = getLayoutInflater (). inflate (R. layout. popup, null);/*** pop-up interface * width: 400 * Height: 200 */final PopupWindow popup = new PopupWindow (root, 400,200); Button btn = (Button) findViewById (R. id. btn); Button close = (Button) findViewById (R. id. close); btn. setOnClickListener (new On ClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub/*** pops up at a specified position. ** the first parameter specifies the PopupWindow anchor view, that is, the view attached. * The second parameter specifies the starting point as the bottom right corner of the parent * The third parameter is set to 0 pixels at the bottom of btn and shifted to the left and top. */Popup. showAtLocation (findViewById (R. id. btn), Gravity. BOTTOM, 0, 0) ;}}); close. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub/*** close PopupWindow */popup. dismiss ();}});}}Activity_main.xml
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <Button android: id = "@ + id/btn" android: layout_width = "200dp" android: layout_height = "wrap_content" android: layout_alignParentTop = "true" android: text = "pop-up"/> <Button android: id = "@ + id/close" android: layout_width = "200dp" android: layout_height = "wrap_content" android: layout_toRightOf = "@ id/btn" android: text = "disabled"/> </RelativeLayout>
Popup. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: id = "@ + id/btn1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "album"/> <Button android: id = "@ + id/btn2" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>
Click here to download the source code!