Android PopupWindow example, androidpopupwindow

Source: Internet
Author: User

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!

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.