Android Bottom Popup box solution (using activity and Popupwindow)

Source: Internet
Author: User

This article mainly discusses how to implement the background of the bottom pop-up cue box for translucent effect. There are two ways to achieve this effect: one is to use the activity settings theme Another way is to use Popupwindow to set the style to achieve the effect.

One, use activity

The first is the layout file for this activity :

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:gravity=" Center_ho Rizontal "android:orientation=" vertical "android:background=" #b0000000 "> <linearlayout Android : layout_marginleft= "10DP" android:layout_marginright= "10DP" android:id= "@+id/pop_layout" android:layout_width= " Fill_parent "android:layout_height=" wrap_content "android:gravity=" Center_horizontal "android:orientation="               Vertical "android:layout_alignparentbottom=" true "android:background=" @drawable/callsharp_select "> <button android:padding= "10DP" android:id= "@+id/btn_take_photo" Android:layout_margi nleft= "30dip" android:layout_marginright= "30dip" android:layout_margintop= "20dip" android:layou   T_width= "Fill_parent"       android:layout_height= "wrap_content" android:text= "photo" android:background= "@drawable/whiteselecto R "android:textstyle=" bold "/> <button android:padding=" 10DP "android:id=" @+id/btn_pick_photo "android:layout_marginleft=" 30dip "android:layout_marginright=" 30dip "Andro          id:layout_margintop= "5dip" android:layout_width= "fill_parent" android:layout_height= "Wrap_content"           android:text= "Select" Android:background= "from the album" @drawable/whiteselector "android:textstyle=" bold " /> <button android:padding= "10DP" android:id= "@+id/btn_cancel" android:layout_m arginleft= "30dip" android:layout_marginright= "30dip" android:layout_margintop= "15dip" Android:la         Yout_marginbottom= "15dip" android:layout_width= "fill_parent" android:layout_height= "Wrap_content"       Android:text= "Cancel"  android:background= "@drawable/whiteselector" android:textstyle= "bold"/> </linearlayou   T> </RelativeLayout>
Configuration in the Androidmanifest
<activity             android:theme= "@style/activity_popup"            android:name= ". Popupactivity "            ></activity>
A custom theme is specified in the style setting
<style name= "Activity_popup" parent= "Android:Theme.Translucent.NoTitleBar" >    <item name= "Android: Windowanimationstyle "> @style/animbottom</item>    </style>
In which I added a very exited animation using the way up and down:

<style name= "Animbottom" parent= "@android: Style/animation" >      <item name= "android:windowenteranimation "> @anim/push_bottom_in</item>      <item name=" android:windowexitanimation "> @anim/push_bottom_out </item>      </style>

The animations for entering and exiting are:

Push_bottom_in:

<?xml version= "1.0" encoding= "Utf-8"?>  <!--up and down--  <set xmlns:android= "/http Schemas.android.com/apk/res/android ">        <translate          android:duration=" Android:fromydelta= "          100%p "          android:toydelta=" 0 "                />         </set>  
Push_bottom_out is:
<?xml version= "1.0" encoding= "Utf-8"?>  <!--up and down--  <set xmlns:android= "/http Schemas.android.com/apk/res/android ">               <translate          android:duration=" $ "          android: Fromydelta= "0"          android:toydelta= "50%p"/>  </set>
This implements the lower popup box.


Second, using Popupwindow to achieve

The style and animation effects used do not change the same as the top, first a custom Popupwindow

Package Com.example.activityanimationdemo;import Android.app.actionbar.layoutparams;import android.app.Activity; Import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.popupwindow;public class Mypopupwindow extends Popupwindow{private View mainview;public MyPopupWindow ( Activity Context,onclicklistener ItemClick) {super (context); Layoutinflater inflater= (Layoutinflater) context.getsystemservice (context. Layout_inflater_service); Mainview=inflater.inflate (r.layout.activity_popup, null); This.setwidth ( layoutparams.match_parent); This.setheight (layoutparams.match_parent); This.setcontentview (Mainview); This.setfocusable (True); This.setanimationstyle (R.style.animbottom);}}
Then the click event in the activity shows Popupwindow:
Findviewbyid (R.id.button1). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO Auto-generated method Stub//activitymy.this.startactivity (New Intent (Activitymy.this, Popupactivity.class)); Popupwindow=new Mypopupwindow (activitymy.this, NULL);p opupwindow.showatlocation (ActivityMy.this.findViewById ( R.id.demo), gravity.bottom| Gravity.center_horizontal, 0, 0);});}
:


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.