Android Custom Popupwindow Small Case _android

Source: Internet
Author: User
Tags xmlns

The

 popupwindow is one of our frequent customers, and it is easy to use.
  wrote the simplest custom Popupwindow, recorded, easy to use later on the above changes can be.

/** * @param * @author LDM * @description Custom Popupwindow * @time 2016/9/29 15:26 * * public class Custompopup extends
  Popupwindow {//contextual private context mcontext;
  Popupwindow Control Click event Callback interface private Ipopuwindowlistener Monclicklistener;
   Popupwindow the button Private button alarm_pop_btn in the layout file; /** * @description Construction method * @author LDM * @time 2016/9/30 9:14 * @param/Public custompopup (Context Mcont
    ext, int width, int height, Ipopuwindowlistener listener) {super (Mcontext);
    This.mcontext = Mcontext;
    This.monclicklistener = listener;
    Get layout file View Mcontentview = Layoutinflater.from (Mcontext). Inflate (R.layout.alarm_disopse_pop, NULL);
    Set Layout Setcontentview (Mcontentview);
    Set the width of the window and height setwidth (width);
    SetHeight (height);
    Sets whether to get focus setfocusable (FALSE);
    Set Popupwindow to enter and exit animation effect Setanimationstyle (R.style.popwindow_exit_anim_style); Settouchable (TRUE); The default is true, set to False, all touch events are not responding, and are PopupwindowThe covered Activity section responds to clicks//sets the window clickable, clicking Popupwindow outside the range, Popupwindow will not disappear setoutsidetouchable (false); External can be clicked, set drawable reason can refer to: http://blog.csdn.net/harvic880925/article/details/49278705 setbackgrounddrawable (
    New Bitmapdrawable ());
  Setting the layout interface of the Initui (); /** * Initialization Window list */private void Initui () {//get to button alarm_pop_btn = (button) Getcontentview (). Findviewbyid
    (R.ID.ALARM_POP_BTN); Set button click event Alarm_pop_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (Vie
        W view) {if (null!= monclicklistener) {monclicklistener.dispose ();
  }
      }
    });
    }/** * Display the Window list interface */public void Show (view view) {int[] location = new INT[2];
    View.getlocationonscreen (location);
  Gravity.bottom is set under View, and can be set according to location to POPUPWINDOWJ display position showatlocation (view, gravity.bottom, 0, 0); /** * @param * @author LDM * @description Click event Callback Processing interface * @time 2016/7/29 15: */public interface Ipopuwindowlistener {void Dispose ();

 }
}

Note: Setting the width of the Popupwindow can also be set by Layoutparams, for example:

 Set the height and width of the Popupwindow by Layoutparams
  viewgroup.layoutparams LP = (viewgroup.layoutparams) Mcontentview.getlayoutparams ();
   lp.widht=400;
   Lp.height = 180;
  MCONTENTVIEW.SETLAYOUTPARAMS (LP);
  But the LP that you get directly to this is probably empty, and when we get the layoutparams of a view, it's usually written like this:
  //Addongloballayoutlistener to get view Layoutparams  mcontentview.getviewtreeobserver (). Addongloballayoutlistener (New Viewtreeobserver.ongloballayoutlistener () {
      @Override public
      void Ongloballayout () {
        viewgroup.layoutparams LP = (viewgroup.layoutparams) Mcontentview.getlayoutparams ();
        Lp.height = 180;
        Lp.width = n;
        MCONTENTVIEW.SETLAYOUTPARAMS (LP);
      }
    );

Used in activity:

Private Custompopup Alarmpopup;
....
Initialize Popupwindow here the Popupwindow height
alarmpopup=new custompopup (getactivity () is set through the data 200来. ViewGroup.LayoutParams.MATCH_PARENT, this); Here this means that the current activity implements the Popupwindow Ipopuwindowlistener interface
// Pop-up Popupwindow
 @Override
  protected void Widgetclick (View v) {
    Super.widgetclick (v);
    Switch (V.getid ()) {case
      r.id.popup:
        alarmpopup.show (v);
        break;
    }
  }
  Process Popupwindow button Click Callback
   @Override public
  void Dispose () {
    //todo sth 
    if (alarmpopup.isshowing ()) {
        Alarmpopup.dismiss ()//close Popupwindow
      }
  }

Popupwindow the corresponding layout interface:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:id=" @+id/pop_ll "
  android:layout_width=" match_parent "
  android:layout_height=" 60DP "
  android:background=" #404040 "
  android:orientation=" Horizontal ">

  <button
    android:id= "@+id/alarm_pop_btn"
    android:layout_width= "match_parent"
    android:layout_height= "Match_parent"
    Android:background= "@null"
    android:gravity= "center"
    android:text= "@string/dispose"
    android: Textcolor= "#FFFFFF"
    android:textsize= "18sp"/>
</LinearLayout>

Animation style:

  <style name= "Popwindow_exit_anim_style" parent= "android:animation" >
    <item name= "Android: Windowenteranimation "> @anim/popshow_anim</item>
    <!--Specifies the animated XML displayed-->
    <item name=" Android : Windowexitanimation "> @anim/pophidden_anim</item>
    <!--specify vanishing animation XML-->
  </style>

Popshow_anim.xml

<?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>

Pophidden_anim.xml:

<?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 above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.