Popupwindow is a custom pop-up window on Android that is handy to use.
The Popupwindow constructor is
Public int int boolean focusable)
Contentview is the width and height of the view,width and height to be displayed, the value is a pixel value, or it can be matcht_parent and wrap_content.
Focusable is a very important parameter for whether you can get the focus, or you can
Public void setfocusable (boolean focusable)
To set, if Focusable is false, in an activity pops up a popupwindow, press the return key, because Popupwindow does not have the focus, will exit the activity directly. If focusable is ejected for True,popupwindow, all touch screens and physical keys are popupwindows processed.
If there is an editor in Popupwindow, Focusable is true.
A simple Popupwindow is implemented below
The layout of the main interface is:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/layout_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " > <ButtonAndroid:id= "@+id/btn_test_popupwindow"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"Android:text= "@string/app_name" /></Relativelayout>
The layout of Popupwindow is:
<? XML version= "1.0" encoding= "Utf-8" ?> < xmlns:android= "http://schemas.android.com/apk/res/android" Android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" Android:background= "#000000"> <TextView Android:layout_width = "Wrap_content" android:layout_height= "80DP" android:text= "@string/app_name" android:textcolor= "#ffffffff" android:layout_centerinparent = "true" android:gravity= "center"/></relativelayout >
The Activity code is:
Public classMainactivityextendsActivity {PrivateButton Mbutton; PrivatePopupwindow Mpopupwindow; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); View Popupview= Getlayoutinflater (). Inflate (R.layout.layout_popupwindow,NULL); Mpopupwindow=NewPopupwindow (Popupview, layoutparams.match_parent, Layoutparams.wrap_content,true); Mpopupwindow.settouchable (true); Mpopupwindow.setoutsidetouchable (true); Mpopupwindow.setbackgrounddrawable (NewBitmapdrawable (Getresources (), (Bitmap)NULL));Mbutton=(Button) Findviewbyid (R.id.btn_test_popupwindow); Mbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Mpopupwindow.showasdropdown (v); } }); }}
These three lines of code
Mpopupwindow.settouchable (true); Mpopupwindow.setoutsidetouchable (true); Mpopupwindow.setbackgrounddrawable (newnull));
The effect is to click on the space when the Popupwindow will disappear
Mpopupwindow.showasdropdown (v);
This line of code shows the Popupwindow as a downward-popping animation
Public void int int Yoff)
The first parameter of this function is a view, we are here a button, then the Popupwindow will be shown below this button, Xoff,yoff is the offset of the display position.
Click on the button and it will show the Popupwindow
Many times we use Popupwindow as a custom menu that requires an effect that pops up from the bottom, which requires animation for Popupwindow.
Create a new Anim folder under Project Res and create a new two XML file in the Anim folder first
Menu_bottombar_in.xml
<? XML version= "1.0" encoding= "Utf-8" ?> < xmlns:android= "http://schemas.android.com/apk/res/android"> <translate android:duration= "$" Android:fromydelta = "100.0%" android:toydelta= "0.0"/></ Set >
Menu_bottombar_out.xml
<? XML version= "1.0" encoding= "Utf-8" ?> < xmlns:android= "http://schemas.android.com/apk/res/android"> <translate android:duration= "$" Android:fromydelta = "0.0" android:toydelta= "100%"/></ Set >
Add a sytle to Res/value/styles.xml
<name= "Anim_menu_bottombar"> <Name = "android:windowenteranimation">@anim/menu_bottombar_in</Item > <name= "Android:windowexitanimation" >@anim/menu_bottombar_out</item> </ style >
Acivity revision changed to
Public classMainactivityextendsActivity {PrivatePopupwindow Mpopupwindow; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); View Popupview= Getlayoutinflater (). Inflate (R.layout.layout_popupwindow,NULL); Mpopupwindow=NewPopupwindow (Popupview, layoutparams.match_parent, Layoutparams.wrap_content,true); Mpopupwindow.settouchable (true); Mpopupwindow.setoutsidetouchable (true); Mpopupwindow.setbackgrounddrawable (NewBitmapdrawable (Getresources (), (Bitmap)NULL)); Mpopupwindow.getcontentview (). Setfocusableintouchmode (true); Mpopupwindow.getcontentview (). Setfocusable (true); Mpopupwindow.getcontentview (). Setonkeylistener (NewOnkeylistener () {@Override Public BooleanOnKey (View V,intKeyCode, KeyEvent event) { if(KeyCode = = Keyevent.keycode_menu && event.getrepeatcount () = = 0 && event.getaction () ==Keyevent.action_down) { if(Mpopupwindow! =NULL&&mpopupwindow.isshowing ()) {Mpopupwindow.dismiss (); } return true; } return false; } }); } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if(KeyCode = = Keyevent.keycode_menu && event.getrepeatcount () = = 0) { if(Mpopupwindow! =NULL&&!mpopupwindow.isshowing ()) {mpopupwindow.showatlocation (Findviewbyid (R.id.layout_main), Gravity.bottom,0, 0); } return true; } return Super. OnKeyDown (KeyCode, event); }}
So click on the menu button to play the definition of Popupwindow, click on the blank or return the key, menu key, Popupwindow will disappear.
Original: http://www.cnblogs.com/sw926/p/3230659.html