"Android Development Diary" Popupwindow perfect Demo

Source: Internet
Author: User

Popupwindow Perfect Demo Implementation
diagram:

Key code Description:

1. Eject the Popupwindow, darken the background

Colordrawable cd = new colordrawable (0x000000);p opuwindow1.setbackgrounddrawable (CD); Windowmanager.layoutparams Lp=getwindow (). GetAttributes (); lp.alpha = 0.4f;getwindow (). SetAttributes (LP);

After the 2.popupwindow disappears, the background restores

public void Ondismiss () {    windowmanager.layoutparams Lp=getwindow (). GetAttributes ();    Lp.alpha = 1f;    GetWindow (). SetAttributes (LP);}

3.popupwindow Rounded Rectangle

Rounded_corners_bg.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >     <solid android:color= "#FFFFFF"/>     <corners         android:topleftradius= "5DP"        android: toprightradius= "5DP"         android:bottomrightradius= "5DP"        android:bottomleftradius= "5DP"/> </shape>
Setting the background in the Pupopwindow layout file

<?

XML version= "1.0" encoding= "UTF-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:layout_ gravity= "center" android:background= "@drawable/rounded_corners_bg" android:orientation= "vertical" > ... ... ... </relativelayout >

4. Click Popupwindow External. Popupwindow will also dismiss

Popuwindow1.setoutsidetouchable (True);p opuwindow1.setfocusable (true);

Full code:

1.mainactivity.java

Package Com.popupwindowdemo;import Android.app.activity;import Android.content.intent;import Android.graphics.drawable.colordrawable;import Android.net.uri;import Android.os.bundle;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; Import Android.view.windowmanager;import Android.widget.button;import Android.widget.popupwindow;import Android.widget.textview;import Android.widget.popupwindow.ondismisslistener;public class MainActivity extends  Activity {//popupwindowprivate Popupwindow popuWindow1; Private View contentview1;private TextView textview1;private Button btn1; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); textview1 = ( TextView) Findviewbyid (r.id.textview2); btn1 = (Button) Findviewbyid (R.id.button1); Btn1.setonclicklistener (new        View.onclicklistener () {public void OnClick (View v) {InitPopuWindow1 (v);    }}); Textview1.setonclicklistener (new View.onclicklistener () {public void OnClick (View v) {final Intent Intent = new Intent (Intent.action_view, Uri.parse ("http://blog.csdn.net/geeklei/article/")); StartActivity (Intent);}}); private void InitPopuWindow1 (View parent) {if (PopuWindow1 = = null) {Layoutinflater Mlayoutinflater = Layoutinflat Er.from (this); contentView1 = Mlayoutinflater.inflate (R.layout.popuwindow1, null);p OpuWindow1 = new Popupwindow (   Contentview1,viewgroup.layoutparams.wrap_content, ViewGroup.LayoutParams.WRAP_CONTENT);   } colordrawable cd = new colordrawable (0x000000);    Popuwindow1.setbackgrounddrawable (CD); Background dimming effect Windowmanager.layoutparams Lp=getwindow (). GetAttributes (); lp.alpha = 0.4f;getwindow (). SetAttributes (LP)        ;   Popuwindow1.setoutsidetouchable (TRUE);   Popuwindow1.setfocusable (TRUE); Popuwindow1.showatlocation (View) parent.getparent (), gravity.center|    Gravity.center_horizontal, 0, 0);    Popuwindow1.update (); Popuwindow1.setondismisslistEner (New Ondismisslistener () {//restore transparency in dismiss public void Ondismiss () {Windowmanager.layoutparams lp=getwind    ow (). GetAttributes ();    Lp.alpha = 1f;    GetWindow (). SetAttributes (LP);  }     });} }

2.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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.popupwindowdemo.MainActivity" > <textview android:id= "@+id/textview1" Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world"/> <Tex TView android:id= "@+id/textview2" android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:layout_alignleft=" @+id/textview1 "android:layout_below=" @+id/textview1 "Android:layout_ margintop= "23DP" android:text= "Click to visit my csdn bLog "android:textcolor=" #1C86EE "/> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:layout_below=" @+id/textview2 "Android:layo Ut_centerhorizontal= "true" android:layout_margintop= "103DP" android:text= "Click to show Popupwindow"/&GT;&L T;/relativelayout>

3.popupwindow1.xml

<?xml version= "1.0" encoding= "UTF-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/     Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:layout_gravity=" center " android:background= "@drawable/rounded_corners_bg" android:orientation= "vertical" > <linearlayout and Roid:layout_width= "220DP" android:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:layou T_gravity= "Center_vertical|center" android:orientation= "vertical" > <textview android:id= "@                +id/group_menu1_passfile "android:layout_width=" fill_parent "android:layout_height=" 40DP " android:layout_marginleft= "20DP" android:gravity= "center_vertical" Android:text                = "Item1" android:textcolor= "#000000" android:textsize= "13sp"/> <view Android:layout_width= "MAtch_parent "android:layout_height=" 0.5DP "android:background=" #999999 "/> &lt ;                TextView android:id= "@+id/group_menu1_playgame" android:layout_width= "Fill_parent" android:layout_height= "40DP" android:layout_marginleft= "20DP" android:gravity= "center _vertical "android:text=" item2 "android:textcolor=" #000000 "android:textsize= "13sp"/> <view android:layout_width= "match_parent" android:layout_height= " 0.5DP "android:background=" #999999 "/> <textview android:id=" @+                 Id/group_menu1_removefriend "android:layout_width=" fill_parent "android:layout_height=" 40DP " android:layout_marginleft= "20DP" android:gravity= "center_vertical" android:t            ext= "Item3"    Android:textcolor= "#000000" android:textsize= "13sp"/> <view android:la            Yout_width= "Match_parent" android:layout_height= "0.5DP" android:background= "#999999"/> <textview android:id= "@+id/group_menu1_setremark" android:layout_width= "Fill_pa Rent "android:layout_height=" 40DP "android:layout_marginleft=" 20DP "android:g                Ravity= "center_vertical" android:text= "Item4" android:textcolor= "#000000" Android:textsize= "13sp"/> <view android:layout_width= "Match_parent" a                ndroid:layout_height= "0.5DP" android:background= "#999999"/> <textview Android:id= "@+id/group_menu1_brokemate" android:layout_width= "Fill_parent" Android:layout_h            eight= "40DP"    android:layout_marginleft= "20DP" android:gravity= "center_vertical" android:text= "ITEM5" android:textcolor= "#000000" android:textsize= "13sp"/> </l Inearlayout></relativelayout>

4.rounded_corners_bg.xml

<?xml version= "1.0" encoding= "Utf-8"?

><shape xmlns:android= "Http://schemas.android.com/apk/res/android" > <solid android:color= "# FFFFFF "/> <corners android:topleftradius=" 5DP " android:toprightradius=" 5DP " android: bottomrightradius= "5DP" android:bottomleftradius= "5DP"/> </shape>


Demo:

http://download.csdn.net/detail/geeklei/7991521


"Android Development Diary" Popupwindow perfect Demo

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.