Hands-on church Popupwindow from the bottom up to the effect of the implementation

Source: Internet
Author: User

Effect, click the Start button, Popwindow from the bottom up, and then click Popwindow Outside, Popwindow and disappear from the top down


As can be seen, the above popupwindow is translucent, I will elaborate on the back.

The basic is the activity_main, very simple, just a button, here I do not post code.

And the next thing is, Popwindow's interface.


The code is as follows: Notice the note in me here.

<?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:layout_margin=" 5DP " android:orientation= "Vertical" > <!--here linearlayout plus android:background= "" This property to be cautious, if added, Popwindow is not translucent- -<button android:id= "@+id/first" android:layout_width= "Match_parent" android:layout_height = "Wrap_content" android:layout_marginbottom= "5DP" android:layout_marginleft= "10DP" Android:layout_mar ginright= "10DP" android:layout_margintop= "5DP" android:background= "@android: Color/holo_red_light" and roid:text= "first button"/> <button android:id= "@+id/second" android:layout_width= "Match_parent" an        droid:layout_height= "Wrap_content" android:layout_marginbottom= "5DP" android:layout_marginleft= "10DP"      android:layout_marginright= "10DP"  Android:background= "@android: Color/holo_red_light" android:text= "second button"/> <button android:id= "@+ Id/third "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:layout_mar Ginbottom= "5DP" android:layout_marginleft= "10DP" android:layout_marginright= "10DP" Android:background = "@android: Color/holo_red_light" android:text= "third button"/></linearlayout>
Then create a new folder Anim under Res/, and Anim create a new two XML file below:


Where the code for Pophidden_anim is as follows

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >     <translate        android:duration= "android:fromydelta="        0 "        android:toydelta=" 50%p "/>    <alpha        android:duration= ""        android:fromalpha= "1.0"        android:toalpha= "0.0"/></set >

The code for Popshow_anim is as follows

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate        android:duration= "android:fromydelta="        100%p "        android:toydelta=" 0 "/>    <alpha        android:duration= ""        android:fromalpha= "0.0"        android:toalpha= "1.0"/></set >

Then add the following code to the Values/styles.xml to look like this, and the ones above are self-bringing.

<resources> <!--Base application theme, dependent on API level.    This theme are replaced by Appbasetheme from Res/values-vxx/styles.xml on newer devices. --<style name= "Appbasetheme" parent= "Android:Theme.Light" > <!--Theme customizations av Ailable in newer API levels can go in res/values-vxx/styles.xml, while customizations related to BAC        Kward-compatibility can go here. -</style> <!--application theme.  --<style name= "Apptheme" parent= "Appbasetheme" > <!--all customizations that is not specific to a Particular api-level can go here. -</style> <!--This is the added code--<style name= "Mypopwindow_anim_style" > <item name= "a Ndroid:windowenteranimation "> @anim/popshow_anim</item> <!--specify animated XML to display--<item name=" Android:wi Ndowexitanimation "> @anim/pophidden_anim</item> <!--Specify the Vanishing animation XML--&Gt </style></resources>

Then the activity inside the code, I have written all the comments, should be able to see clearly


Package Com.example.popwindow;import Android.os.bundle;import Android.app.activity;import android.content.Context; Import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.colordrawable;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.menu;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.windowmanager;import Android.widget.button;import Android.widget.popupwindow;import Android.widget.popupwindow.ondismisslistener;import Android.widget.Toast; public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button start = (Button) Findviewbyid (R.id.start), Start.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {Showpopwindow ();}});} /** * Show Popupwindow */private void Showpopwindow () {//Use Layoutinflater to get viewlayoutinflater Inflater = (LayoutInflater) g EtsyStemservice (Context.layout_inflater_service); View view = Inflater.inflate (r.layout.popwindowlayout, NULL);//The following are two ways to get width and height getWindow (). Getdecorview (). GetWidth () Popupwindow window = new Popupwindow (VIEW,WINDOWMANAGER.LAYOUTPARAMS.MATCH_PARENT,WINDOWMANAGER.LAYOUTPARAMS.WRAP_ CONTENT);//Set Popwindow popup form clickable, this sentence must be added, and is truewindow.setfocusable (true);//Instantiate a colordrawable color as translucent colordrawable DW = new colordrawable (0xb0000000); window.setbackgrounddrawable (DW);//set Popwindow display and vanishing animation Window.setanimationstyle (r.style.mypopwindow_anim_style);//Window.showatlocation (MainActivity.this.findViewById (R.id.start) at the bottom, Gravity.bottom, 0, 0);//Check if the button in the Popwindow can be clicked button First = (Button) View.findviewbyid (R.id.first); First.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {System.out.println ("the first button was clicked" );}});/ /popwindow Vanishing Listener Method Window.setondismisslistener (new Ondismisslistener () {@Overridepublic void Ondismiss () { System.out.println ("Popwindow disappears");}});}}

where Window.setfocusable (true) and window.setbackgrounddrawable () must be filled, if you want to make Popwindow translucent, that is the method above,

If just call this method simply write Window.setbackgrounddrawable (new bitmapdrawable ());


You can see how this blog Popupwindow displayed in the specified location for the specific method of Popupwindow display location


This resource I have uploaded, 0 points to download, there is a need to download a look click here to download


Hands-on church Popupwindow from the bottom up to the effect of the 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.