Popupwindow is a custom pop-up window on Android that's handy to use.
The Popupwindow constructor is
Copy Code code as follows:
Public Popupwindow (View contentview, int width, int height, 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 whether the focus is available, this is an important parameter, or it can be set by public void Setfocusable (Boolean focusable), 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 buttons have popupwindows processing.
Focusable to be true if there is editor in the Popupwindow.
The following implementation of a simple Popupwindow
The layout of the main interface are:
<relativelayout xmlns: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 ">
<button
android: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"?> <relativelayout 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 code for the activity is:
public class Mainactivity extends activity {
private Button Mbutton;
Private Popupwindow Mpopupwindow;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
View Popupview = Getlayoutinflater (). Inflate (R.layout.layout_popupwindow, null);
Mpopupwindow = new Popupwindow (Popupview, Layoutparams.match_parent, layoutparams.wrap_content, true);
Mpopupwindow.settouchable (true);
Mpopupwindow.setoutsidetouchable (true);
Mpopupwindow.setbackgrounddrawable (New Bitmapdrawable (Getresources (), (BITMAP) null));
Mbutton = (Button) Findviewbyid (R.id.btn_test_popupwindow);
Mbutton.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
Mpopupwindow.showasdropdown (v);}}
);
}
These three lines of code
Mpopupwindow.settouchable (true);
Mpopupwindow.setoutsidetouchable (true);
Mpopupwindow.setbackgrounddrawable (New Bitmapdrawable (Getresources (), (BITMAP) null));
The effect is to click on the blank space when the Popupwindow will disappear.
Mpopupwindow.showasdropdown (v);
This line of code displays the Popupwindow as a pop-up animation.
public void Showasdropdown (View anchor, int xoff, int yoff)
The first argument for this function is a view, and we have a button here, so Popupwindow will be shown below this button, Xoff,yoff the offset of the display position.
Clicking on the button will show the Popupwindow
Many times we use Popupwindow as a custom menu, and we need an effect that pops up from the bottom, which requires animation for Popupwindow.
Create a new Anim folder under Engineering res, create two XML files in the Anim folder first
Menu_bottombar_in.xml
<?xml version= "1.0" encoding= "Utf-8"?> <set 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"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<translate
android:duration="
android:fromydelta= "0.0"
android: Toydelta= "100%"/>
</set>
Add a sytle to the Res/value/styles.xml
<style name= "Anim_menu_bottombar" >
<item name= "android:windowenteranimation" > @anim/menu_ bottombar_in</item>
<item name= "android:windowexitanimation" > @anim/menu_bottombar_out</item >
</style>
Acivity modified to
public class Mainactivity extends activity {private Popupwindow Mpopupwindow;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
View Popupview = Getlayoutinflater (). Inflate (R.layout.layout_popupwindow, NULL);
Mpopupwindow = new Popupwindow (Popupview, Layoutparams.match_parent, layoutparams.wrap_content, true);
Mpopupwindow.settouchable (TRUE);
Mpopupwindow.setoutsidetouchable (TRUE);
Mpopupwindow.setbackgrounddrawable (New Bitmapdrawable (Getresources (), (BITMAP) null));
Mpopupwindow.getcontentview (). Setfocusableintouchmode (True);
Mpopupwindow.getcontentview (). Setfocusable (True); Mpopupwindow.getcontentview (). Setonkeylistener (New Onkeylistener () {@Override public boolean onkey View v, int KEYC Ode, KeyEvent Event) {if (keycode = = Keyevent.keycode_menu && event.getrepeatcount () = 0 && Ev Ent.getaction () = = Keyevent.action_down) {if (MPOpupwindow!= null && mpopupwindow.isshowing ()) {Mpopupwindow.dismiss ();
return true;
return false;
}
}); @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_menu && E Vent.getrepeatcount () = = 0) {if (Mpopupwindow!= null &&!mpopupwindow.isshowing ()) {Mpopupwindow.showatl
Ocation (Findviewbyid (R.id.layout_main), Gravity.bottom, 0, 0);
return true;
Return Super.onkeydown (KeyCode, event);
}
}
Click on the menu button will play out of the definition of Popupwindow, click on the blank or return keys, menu keys, Popupwindow will disappear.
If there is something wrong with the article, I hope you can understand it.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.