Android Custom Popupwindow the pop-up menu effect in the upper right corner of the imitation micro-letter _android

Source: Internet
Author: User
Tags event listener set background


Micro-letter in the upper right corner of the Operation menu looks very good, just imitate a bit, but the old version of the micro-letter, hand just have some old version of the micro-letter resources icon, to share with you.



Do not know what the micro-letter is implemented, I use Popupwindow to achieve, mainly divided into several pieces of content:



1. Window Layout file: Popwin_share.xml


<!--?</span--> The XML version = "1.0" encoding = "utf-8"? >
The < LinearLayout XMLNS: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/title_tools_bg"
Android: orientation = "vertical" >

The < LinearLayout
The android: id = "@ + id/layout_share"
Style = "@ style/fill_width"
The android: orientation = "horizontal"
Android: background = "@ drawable/menu_left_item_selector"
Android: padding = "5 dp" >

The < ImageView
Android: layout_marginLeft = "7 dp"
Style = "@ style/wrap"
Android: scaleType = "fitCenter"
The android: SRC = "@ drawable/share" / >

The < TextView
Style = "@ style/wrap"
Android: textColor = "@ color/white"
Android: textSize = "@ dimen/text18"
Android: layout_marginLeft = "5 dp"
Android :text=" share content "/>
< / LinearLayout >

The < LinearLayout
The android: id = "@ + id/layout_copy"
Style = "@ style/fill_width"
The android: orientation = "horizontal"
Android: background = "@ drawable/menu_left_item_selector"
Android: padding = "5 dp" >

The < ImageView
Android: layout_marginLeft = "5 dp"
Style = "@ style/wrap"
Android: scaleType = "fitCenter"
The android: SRC = "@ drawable/copy_pressed" / >

The < TextView
Style = "@ style/wrap"
Android: textColor = "@ color/white"
Android: textSize = "@ dimen/text18"
Android: layout_marginLeft = "5 dp"
Android :text=" copy result "/>
< / LinearLayout >

< / LinearLayout >


Linear layout, because the inside is a row of rows of vertical menu, the linear layout easier to control. The large layout contains two vertically arranged linear layouts, each of which has a imageview and textview arranged horizontally, with a simple layout. Large layout background with a picture, of course, you can customize some other colors.



2, Popupwindow code, I am here to customize a Popupwindows class, inherited from Popupwindow:


 Package Com.xjw.view; 
 
Import COM.XJW.TRANSLATE.R; 
Import android.app.Activity; 
Import android.graphics.drawable.ColorDrawable; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.widget.AdapterView; 
Import Android.widget.BaseAdapter; 
Import Android.widget.LinearLayout; 
Import Android.widget.ListView; 
  Import Android.widget.PopupWindow; /** * Project Name: Translate * Implementation function: Translation details interface, sharing pop-up window * Class Name: Popwinshare * Class Description: (the main function of the class) * Creator: Xu Jiwei * E-MAIL:XUJIWEI5  58@126.com * Create time: October 18, 2014 PM 4:37:25 * @version * * public class Popwinshare extends popupwindow{private 
  View Mainview; 
 
 Private LinearLayout layoutshare, layoutcopy; 
     Public Popwinshare (Activity paramactivity, View.onclicklistener paramonclicklistener, int paramInt1, int paramInt2) { 
     Super (paramactivity); 
    Window Layout Mainview = Layoutinflater.from (paramactivity). Inflate (r.layout.popwin_share, NULL); Share Layout Layoutshare = ((linearlayout) Mainview.findViewbyid (R.id.layout_share)); 
    Copy Layout layoutcopy = (linearlayout) Mainview.findviewbyid (r.id.layout_copy); 
      Sets the event listener if (Paramonclicklistener!= null) {Layoutshare.setonclicklistener (Paramonclicklistener) for each child layout; 
    Layoutcopy.setonclicklistener (Paramonclicklistener); 
    } setcontentview (Mainview); 
    Set width setwidth (paramInt1); 
    Set height setheight (paramInt2); 
    Set Display hidden animation setanimationstyle (r.style.animtools); 
  Set background transparent setbackgrounddrawable (new colordrawable (0)); 
 } 
}Package com. XJW. View;

The import com. XJW. Translate. R;

The import android. App. The Activity;
The import android. Graphics. Drawable. ColorDrawable;
The import android. View. LayoutInflater;
The import android. View. The view;
The import android. Widget. AdapterView;
The import android. Widget. BaseAdapter;
The import android. Widget. LinearLayout;
The import android. Widget. ListView.
The import android. Widget. PopupWindow;
/ * *
* project name: translate
* function: translate details interface, share popup window
* class name: PopWinShare
* class description :(main functions of this class)
* founder: xu jiwei
* E-mail: xujiwei558@126.com
* created at 4:37:25 PM on October 18, 2014
* @ version
* /
Public class PopWinShare extends PopupWindow{
Private View mainView.
Private LinearLayout layoutShare layoutCopy;

Public PopWinShare(Activity paramActivity, view. OnClickListener paramOnClickListener, int paramInt1, int paramInt2){
Super (paramActivity);
// window layout
MainView = LayoutInflater. The from (paramActivity). Inflate (R.l ayout. Popwin_share, null);
// share layout
LayoutShare = ((LinearLayout) mainView. The findViewById (R.i d.l ayout_share));
// copy the layout
LayoutCopy = (LinearLayout) mainView. The findViewById (R.i d.l ayout_copy);
// sets the event listener for each sublayout
If (paramOnClickListener! = null) {
LayoutShare. SetOnClickListener (paramOnClickListener);
LayoutCopy. SetOnClickListener (paramOnClickListener);
}
The setContentView (mainView);
// set the width
SetWidth (paramInt1);
// set the height
SetHeight (paramInt2);
// set to show hidden animation
SetAnimationStyle (R.s tyle. AnimTools);
// set background transparency
SetBackgroundDrawable (new ColorDrawable (0));
}
} 


It provides a way to construct contains four parameters, the first parameter is the activity of the context, the second is the click of the menu event, from the outside, to bind to each row of the menu, the specific event implementation of course to write in the activity, the following two are the width and height of the pop-up window. It also contains an animated style that appears when the window is open and closed.



3, animation style, display animation, zoom Animation: Push_in.xml


<?xml version= "1.0" encoding= "Utf-8"?> <scale xmlns:android=  "Http://schemas.android.com/apk/res" /android " 
    android:interpolator=" @android: Anim/accelerate_decelerate_interpolator "  
    android:fromxscale=" 1.0 "   
    android:toxscale=" 1.0 "   
    android:fromyscale=" 0 "   
    android:toyscale=" 1.0 "   
    android:pivotx=" 0 "  
    android:pivoty= "10%"  
    


Turn off animation and zoom animation: Push_out.xml


 <?xml version= "1.0" encoding= "Utf-8"?> <scale xmlns:android=  "Http://schemas.android.com/apk/res" /android " 
    android:interpolator=" @android: Anim/accelerate_decelerate_interpolator "  
    android:fromxscale=" 1.0 "   
    android:toxscale=" 1.0 "   
    android:fromyscale=" 1.0 "   
    android:toyscale=" 0 "   
    android:pivotx=" 0 "  
    android:pivoty= "10%"  
    android:duration= "/>"  


Style style definition:


 <style name= "Animtools" parent= "@android: Style/animation" > 
   <item name= "android:windowenteranimation "> @anim/push_in</item> 
   <item name=" android:windowexitanimation "> @anim/push_out</item> 
 </style> 


So far, our custom window has been defined. Next look at the use.


 if (Popwinshare = = null) { 
  //Custom Click event 
  onclicklintener paramonclicklistener = new Onclicklintener (); 
  Popwinshare = new Popwinshare (Translatedatacontentactivity.this, Paramonclicklistener, DisplayUtil.dip2px (context, 160), displayutil.dip2px (context, 160)); 
  Monitor the focus of the window event, and then click outside the window to suppress the display of 
  Popwinshare.getcontentview (). Setonfocuschangelistener (New View.onfocuschangelistener () { 
     
    @Override public 
    void Onfocuschange (View V, boolean hasfocus) { 
      if (!) Hasfocus) { 
        Popwinshare.dismiss ();}}} 
  ); 
Sets the default get Focus 
popwinshare.setfocusable (true); 
Displays the window at the offset of the x and Y of a control 
Popwinshare.showasdropdown (btntools, 0, 0); 
If the window exists, update the 
popwinshare.update (); 


Click events for each submenu customize the inner class, in which you can write a click event for each submenu.


Class Onclicklintener implements onclicklistener{ 
 
    @Override public 
    void OnClick (View v) { 
      switch (V.getid ( ) {case 
      r.id.layout_share: Break 
         
        ; 
      Case r.id.layout_copy: Break 
         
        ; 
          
      Default: Break;}}} 
     
   


Effect preview:






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.