Objective
In the H5 fiery era, many frameworks are out of the bottom window of the control, in H5 is called pop-up menu Actionsheet, today we also imitate an iOS bottom window, based on the Apple QQ selection avatar function.
Body
Don't say much nonsense, first of all, to achieve today's effect chart
Opening code for the entire Popupwindow
private void Openpopupwindow (View v) {
//Prevent repeat Press button
if (Popupwindow!= null && popupwindow.isshowing ()) { C3/>return;
}
Set Popupwindow view View
= Layoutinflater.from (this). Inflate (R.layout.view_popupwindow, null);
Popupwindow = new Popupwindow (view, RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP _content);
Set the background, this has no effect, does not add will error
popupwindow.setbackgrounddrawable (new bitmapdrawable ());
Set the click window to hide itself
popupwindow.setfocusable (true);
Popupwindow.setoutsidetouchable (true);
Set animation
Popupwindow.setanimationstyle (R.style.popupwindow);
Set position
popupwindow.showatlocation (V, gravity.bottom, 0, navigationheight);
Set Vanishing Listening
popupwindow.setondismisslistener (this);
Set Popupwindow View Click event
setonpopupviewclick (view);
Set Background color
setbackgroundalpha (0.5f);
}
Step Analysis:
Layout of the Popupwindow
Creation of Popupwindow
Popupwindow Add animation effect
Popupwindow Set Background Shadow
Popupwindow Listener Click event
Get the height of the Navigationbar
Popupwindow layout: In layout, design the layout we need
<?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 " > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_ Margin= "8DP" android:background= "@drawable/popup_shape" android:orientation= "vertical" > <textview android: Layout_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "center" android:padding= "16DP" android:text= "You can upload photos to the photo wall" android:textcolor= "#666" android:textsize= "14sp"/> <view android:layout_width= " Match_parent "android:layout_height=" 0.1px "android:background=" #888 "/> <textview android:id=" @+id/tv_pick_ Phone "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:gravity=" Center "android: padding= "16DP" android:text= "select" Android:textcolor= "from Mobile album" "Android:textsize=" #118 "18sp "/> <view android:layout_width=" match_parent "android:layout_height=" 0.1px "android:background=" #888 "/&G
T <textview android:id= "@+id/tv_pick_zone" android:layout_width= match_parent "android:layout_height=" Wrap_ Content "android:gravity=" center "android:padding=" 16DP "android:text=" from Space album selection "android:textcolor=" #118 "Android: Textsize= "18sp"/> </LinearLayout> <linearlayout android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:layout_margin= "8DP" android:background= "@drawable/popup_shape" > <textview Android:id= "@+id/tv_cancel" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android: gravity= "center" android:padding= "16DP" android:text= "Cancel" android:textcolor= "#118" android:textsize= "18sp" Android:
textstyle= "Bold"/> </LinearLayout> </LinearLayout>
The effect is:
Popupwindow creation: This creation is the same as our normal control creation method
Set Popupwindow view View
= Layoutinflater.from (this). Inflate (R.layout.view_popupwindow, null);
Popupwindow = new Popupwindow (view, RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP _content);
Popupwindow Add animation effect: We create a Anim folder, create our out and in animation effects, and then add our animations in style
<?xml version= "1.0" encoding= "Utf-8"?>
<!--into animation--> <translate xmlns:android=
"http:// Schemas.android.com/apk/res/android "android:interpolator=" @android: Anim/accelerate_interpolator "android: Fromydelta= "100%" android:toydelta= "0" android:duration= "/> <?xml version=" 1.0 "encoding="
? >
<!--exit animation-->
<translate xmlns:android= "Http://schemas.android.com/apk/res/android" Android: Interpolator= "@android: Anim/accelerate_interpolator" android:fromydelta= "0" android:toydelta= "100%" Android: duration=/>
<!--flick window animation-->
<style name= "Popupwindow" > <item name= "android: Windowenteranimation "> @anim/window_in</item> <item name=" Android:windowexitanimation "> @anim Window_out</item> </style>
Set animation
Popupwindow.setanimationstyle (R.style.popupwindow);
Popupwindow Set Background shadow: When the window is open, set the transparency to 0.5, the window disappears when the transparency is set to 1
Set screen background transparency effect public
void Setbackgroundalpha (float alpha) {
windowmanager.layoutparams LP = GetWindow (). GetAttributes ();
Lp.alpha = Alpha;
GetWindow (). SetAttributes (LP);
}
Popupwindow Listener Click event: Monitor the click events of controls inside our Popupwindow
Set Popupwindow View Click event
setonpopupviewclick (view);
private void Setonpopupviewclick (view view) {
TextView tv_pick_phone, Tv_pick_zone, Tv_cancel;
Tv_pick_phone = (TextView) View.findviewbyid (r.id.tv_pick_phone);
Tv_pick_zone = (TextView) View.findviewbyid (r.id.tv_pick_zone);
Tv_cancel = (TextView) View.findviewbyid (r.id.tv_cancel);
Tv_pick_phone.setonclicklistener (this);
Tv_pick_zone.setonclicklistener (this);
Tv_cancel.setonclicklistener (this);
}
Get the height of the Navigationbar: This needs to be adapted. Some mobile phones do not have navigationbar some mobile phones have, here with the existence of Navigationbar to demonstrate
int ResourceID = Getresources (). Getidentifier ("Navigation_bar_height", "Dimen", "Android");
Navigationheight = Getresources (). Getdimensionpixelsize (ResourceID);
On a cell phone that exists Navigationbar, set its popupwindow position.
//Set Location
Popupwindow.showatlocation (V, gravity.bottom, 0, navigationheight);
On the phone without Navigationbar, set its popupwindow position
//Set Location
Popupwindow.showatlocation (V, gravity.bottom, 0, 0);
Source
Github:https://github.com/androidhensen/iospopupwindow
public class Mainactivity extends Appcompatactivity implements View.onclicklistener, Popupwindow.ondismisslistener {p
Rivate Button Bt_open;
Private Popupwindow Popupwindow;
private int navigationheight;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Bt_open = (Button) Findviewbyid (R.id.bt_open);
Bt_open.setonclicklistener (this);
int ResourceID = Getresources (). Getidentifier ("Navigation_bar_height", "Dimen", "Android");
Navigationheight = Getresources (). Getdimensionpixelsize (ResourceID); @Override public void OnClick (View v) {switch (V.getid ()) {Case R.id.bt_open:openpopupwindow (v
);
Break
Case R.id.tv_pick_phone:toast.maketext (This, "Select from Cell Phone album", Toast.length_short). Show ();
Popupwindow.dismiss ();
Break Case R.id.tv_pick_zone:toast.maketext (This, "Select from Space album", Toast.length_short). Show ();
Popupwindow.dismiss ();
Break
Case R.id.tv_cancel:popupwindow.dismiss ();
Break } private void Openpopupwindow (View v) {//Prevent repeat Press button if (Popupwindow!= null && popupwindow.isshowing
()) {return;
//Set Popupwindow View view = Layoutinflater.from (this). Inflate (R.layout.view_popupwindow, NULL); Popupwindow = new Popupwindow (view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CON
TENT);
Set the background, this has no effect, does not add will error popupwindow.setbackgrounddrawable (new bitmapdrawable ());
Set the click window to hide itself popupwindow.setfocusable (true);
Popupwindow.setoutsidetouchable (TRUE);
Set animation Popupwindow.setanimationstyle (R.style.popupwindow);
Set position popupwindow.showatlocation (V, gravity.bottom, 0, navigationheight);
Set Vanishing Listening popupwindow.setondismisslistener (this);
Set Popupwindow View Click event Setonpopupviewclick (view); Set Background color SetbackgrouNdalpha (0.5f);
} private void Setonpopupviewclick (view view) {TextView tv_pick_phone, tv_pick_zone, Tv_cancel;
Tv_pick_phone = (TextView) View.findviewbyid (R.id.tv_pick_phone);
Tv_pick_zone = (TextView) View.findviewbyid (R.id.tv_pick_zone);
Tv_cancel = (TextView) View.findviewbyid (r.id.tv_cancel);
Tv_pick_phone.setonclicklistener (this);
Tv_pick_zone.setonclicklistener (this);
Tv_cancel.setonclicklistener (this); //Set screen background transparency effect public void Setbackgroundalpha (float Alpha) {windowmanager.layoutparams LP = GetWindow (). Getattri
Butes ();
Lp.alpha = Alpha;
GetWindow (). SetAttributes (LP);
@Override public void Ondismiss () {setbackgroundalpha (1);
}
}
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.