Display and close of the Popupwindow of the Android Master Road

Source: Internet
Author: User

The effect of Popwindow is similar to a fixed small window. Just look at the effect.

Effect:



Main code:

Package Com.example.popupwindowdemo;import Android.os.bundle;import Android.app.activity;import Android.app.actionbar.layoutparams;import Android.util.log;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.popupwindow;public Class Mainactivity extends Activity implements Onclicklistener{private Button btn_show,btn_close;private Popupwindow Mpopupwindow; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); btn_show = (Button) Findviewbyid (r.id.btn_show); btn_ Show.setonclicklistener (this), Btn_close = (Button) Findviewbyid (r.id.btn_close); Btn_close.setonclicklistener (this );} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btn_show:if (null = = Mpopupwindow | |!mpopupwindow.isshowi Ng ()) {Layoutinflater Mlayoutinflater = (layoutinflater) this. Getsystemservice (Layout_inflater_service);  View music_popunwindwow = mlayoutinflater.inflate (R.layout.music_popwindow, NULL); Mpopupwindow = new Popupwindow (music_popunwindwow,layoutparams.match_parent,layoutparams.wrap_content); Mpopupwindow.showatlocation (Findviewbyid (r.id.main), gravity.right| Gravity.bottom, 0, 0);} Break;case r.id.btn_close:if (null! = Mpopupwindow && mpopupwindow.isshowing ()) {Mpopupwindow.dismiss (); if ( NULL = = Mpopupwindow) {log.e ("mainactivity", "null = = Mpopupwindow");}} Break;default:break;}}}

Activity_main.xml:

<linearlayout 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 "    android:id= "@+id/main"    android:orientation= "horizontal"    >    <button        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:id= "@+id/btn_show"        android:text = "Show"/>    <button         android:layout_width= "wrap_content"        android:layout_height= "wrap_content "        android:id=" @+id/btn_close "        android:text=" Close "/></linearlayout>

Music_popwindow.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= "Wrap_content"
Android:background= "#77777777"
android:orientation= "Vertical" >
<button android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_gravity= "Right"
android:text= "Like"/>


</LinearLayout>
<?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=" Wrap_content "    android: Background= "#77777777"    android:orientation= "vertical" >    <button android:layout_width= "wrap_content "        android:layout_height=" wrap_content "        android:layout_gravity=" right "        android:text=" like "/>< /linearlayout>


Display and close of the Popupwindow of the Android Master Road

Related Article

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.