Popup Popupwindow background Dimming implementation

Source: Internet
Author: User

popup Popuowindow After the code is set is Popupwindow default to get focus so popupwindow display when other control click is unresponsive

The method used is

Pwmypopwindow.setfocusable (TRUE);

The Code also sets the

   pwmypopwindow.setbackgrounddrawable ( This.getresources (). getdrawable (   
R.mipmap.ic_launcher)) ;//Set background picture, cannot be set in layout, to be set by code
pwmypopwindow.setoutsidetouchable (true) ; Touch Popupwindow outside, Popupwindow disappears. This requires your popupwindow to have a background image to succeed



is used
Windowmanager.layoutparams LP = GetWindow (). GetAttributes ();
Lp.alpha = Bgalpha; 0.0-1.0
GetWindow (). SetAttributes (LP);

Here is the code
<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"Tools:context=". Mainactivity "Android:background= "#ffffff" > <LinearLayout Android:id= "@+id/ll_head_bar"Android:layout_width= "Wrap_content"Android:layout_height= "400DP"android:orientation= "vertical" > <!--this picture button pops up when pressed popupwindow--> <ImageButton Android:id= "@+id/ib_operate_more"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@mipmap/ic_launcher"/>
<!--This button is tested when Poupwindow is displayed, clicking this button does not respond because this button does not get focus--<Button android:layout_margintop= "200DP"android:layout_gravity= "Bottom"Android:id= "@+id/ib_operate_more2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Test Button"/> </LinearLayout></RelativeLayout>
 <?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 : Gravity  = "center"  android:orientation  =" vertical "> <textview android:id  = "@+id/tv_list_item"  android:layout_width  = "M Atch_parent " android:layout_height  =" Match_parent " android:gravity  = "center"  Android:minheight  =" 40DP " Android:minwidth  = "120DP"  Android:textsize  =" 20SP "/>& Lt;/linearlayout> 
list_item_popupwindow.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 : Orientation  = "vertical"  Android:background  = "#d93c58b3" > " <listview android:id  =" @+id/lv_popup_list " Android:layout_width  = "Match_parent"  Android:layout_height  =" mat        Ch_parent " Android:listselector  =" #00000000 " = "true"  Android:background  =" #d42a2a " /></linearlayout> 
Task_detail_popupwindow.xml

 PackageCom.ceshi.popupwindow;Importandroid.app.Activity;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.view.WindowManager;ImportAndroid.widget.AdapterView;ImportAndroid.widget.Button;ImportAndroid.widget.ImageButton;ImportAndroid.widget.ListView;ImportAndroid.widget.PopupWindow;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.Toast;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map; Public classMainactivityextendsActivity {PrivateImageButton Iboperationmore; PrivateButton IbOperationMore2; List<map<string, string>>morelist; PrivatePopupwindow Pwmypopwindow;//Popupwindow    PrivateListView lvpopuplist;//ListView in the Popupwindow    Private intNum_of_visible_list_rows = 3;//Specify the number of item in Popupwindow@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Inidata ();        Inipopupwindow (); //More action ButtonsIboperationmore =(ImageButton) Findviewbyid (R.id.ib_operate_more); //IbOperationMore2 is used to test the focus problem so that the other controls are not clickable when the Popupwindow is displayed//In other words, the other controls don't respond when clickedIbOperationMore2 =(Button) Findviewbyid (R.id.ib_operate_more2); Iboperationmore2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (mainactivity. This, "Cao", Toast.length_short). Show ();        }        }); Iboperationmore.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(Pwmypopwindow.isshowing ()) {Pwmypopwindow.dismiss ();//Close}Else{pwmypopwindow.showasdropdown (iboperationmore);//ShowBackgroundalpha (0.7f);    }            }        }); }    Private voidInidata () {morelist=NewArraylist<map<string, string>>(); Map<string, string>map; Map=NewHashmap<string, string>(); Map.put ("Share_key", "Copy");        Morelist.add (map); Map=NewHashmap<string, string>(); Map.put ("Share_key", "delete");        Morelist.add (map); Map=NewHashmap<string, string>(); Map.put ("Share_key", "Modify");    Morelist.add (map); }    Private voidInipopupwindow () {layoutinflater inflater= (Layoutinflater) This. Getsystemservice (Layout_inflater_service); View Layout= Inflater.inflate (R.layout.task_detail_popupwindow,NULL); Lvpopuplist=(ListView) Layout.findviewbyid (r.id.lv_popup_list); Pwmypopwindow=NewPopupwindow (Layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); Pwmypopwindow.setfocusable (true);//Add the ListView in this Popupwindow to receive the Click eventLvpopuplist.setadapter (NewSimpleadapter (mainactivity. This, Morelist, R.layout.list_item_popupwindow,NewString[] {"Share_key" },                New int[] {r.id.tv_list_item}); Lvpopuplist.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {Pwmypopwindow.dismiss (); Toast.maketext (mainactivity. This, Morelist.get (position). Get ("Share_key"), Toast.length_long). Show ();        }        }); //control Popupwindow width and height adaptive        /*lvpopuplist.measure (View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);        Pwmypopwindow.setwidth (Lvpopuplist.getmeasuredwidth ()); Pwmypopwindow.setheight ((Lvpopuplist.getmeasuredheight () +) * num_of_visible_list_rows);*/        //control Popupwindow Click on the screen other places disappearPwmypopwindow.setbackgrounddrawable ( This. Getresources (). getdrawable (R.mipmap.ic_launcher));//set the background picture, cannot be set in the layout, to be set by codePwmypopwindow.setoutsidetouchable (true);//Touch Popupwindow outside, Popupwindow disappears. This requires your popupwindow to have a background image to be successful, as onPwmypopwindow.setondismisslistener (NewPopupwindow.ondismisslistener () {@Override Public voidOndismiss () {//when Popupwindow disappears, it reverts to its original transparency.Backgroundalpha (1f);    }        }); }    /*** Set the background transparency of the add screen *@paramBgalpha*/     Public voidBackgroundalpha (floatBgalpha) {Windowmanager.layoutparams LP=GetWindow (). GetAttributes (); Lp.alpha= Bgalpha;//0.0-1.0GetWindow (). SetAttributes (LP); }}
mainactivity







Popup Popupwindow background Dimming implementation

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.