Android-custom PopupWindow
May 12, 2014PopupWindow can be seen everywhere in applications and is often used. For example, PopupWindow is used in the old version, which is customized. Someone has already imitated the ActionBar of the new version 5.2, but it is not clear whether ActionBar is used or other authors, this blog introduces how to customize a PopupWindow for your application development. Because I recently used this knowledge point in the development of an application, I achieved the same effect as the new version.Source code download: http://download.csdn.net/detail/wwj_748/7337175As follows:
First, start with the layout/14_CustomPopupWindow/res/layout/activity_swipe.xml
/14_CustomPopupWindow/res/layout/add_popup_dialog.xml
/14_CustomPopupWindow/res/layout/more_popup_dialog.xml
The above are the home page and two popupwindows la S.
The following two custom popupwindows are used to encapsulate the desired PopuoWindow. Here is an example.
/14_CustomPopupWindow/src/com/wwj/popupwindow/AddPopWindow. java
Package com. wwj. popupwindow; import android. app. activity; import android. content. context; import android. graphics. drawable. colorDrawable; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup. layoutParams; import android. widget. linearLayout; import android. widget. popupWindow;/*** custom popupWindow *** @ author wwj ***/public clas S AddPopWindow extends PopupWindow {private View conentView; public AddPopWindow (final Activity context) {LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); conentView = inflater. inflate (R. layout. add_popup_dialog, null); int h = context. getWindowManager (). getdefadisplay display (). getHeight (); int w = context. getWindowManager (). getdefadisplay display (). getWidth (); // Set the Viewthis of SelectPicPopupWindow. setContentView (conentView); // set the width of the SelectPicPopupWindow pop-up form this. setWidth (w/2 + 50); // you can specify the height of the SelectPicPopupWindow window. setHeight (LayoutParams. WRAP_CONTENT); // you can click this to set the SelectPicPopupWindow pop-up window. setFocusable (true); this. setOutsideTouchable (true); // refresh the status this. update (); // instantiate a ColorDrawable color as translucent ColorDrawable dw = new ColorDrawable (0000000000); // click the back key and other places to make it disappear and set This will trigger OnDismisslistener, and set other control changes and other operations this. setBackgroundDrawable (dw); // mPopupWindow. setAnimationStyle (android. r. style. animation_Dialog); // sets SelectPicPopupWindow to bring up the form animation effect this. setAnimationStyle (R. style. animationPreview); LinearLayout addTaskLayout = (LinearLayout) conentView. findViewById (R. id. add_task_layout); LinearLayout teamMemberLayout = (LinearLayout) conentView. findViewById (R. id. team_member_layou T); addTaskLayout. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {AddPopWindow. this. dismiss () ;}}); teamMemberLayout. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {AddPopWindow. this. dismiss () ;}}) ;}/ *** show popupWindow ** @ param parent */public void showPopupWindow (View parent) {if (! This. isShowing () {// The following PULL mode shows popupwindowthis. showAsDropDown (parent, parent. getLayoutParams (). width/2, 18);} else {this. dismiss ();}}}
/14_CustomPopupWindow/src/com/wwj/popupwindow/MorePopWindow. java
Package com. wwj. popupwindow; import android. app. activity; import android. content. context; import android. graphics. drawable. colorDrawable; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup. layoutParams; import android. widget. popupWindow; public class MorePopWindow extends PopupWindow {private View conentView; public MorePopWindow (final Activity context) {LayoutInflat Er inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); conentView = inflater. inflate (R. layout. more_popup_dialog, null); int h = context. getWindowManager (). getdefadisplay display (). getHeight (); int w = context. getWindowManager (). getdefadisplay display (). getWidth (); // set the Viewthis of SelectPicPopupWindow. setContentView (conentView); // set the width of the SelectPicPopupWindow pop-up form this. setWidth (w/2 + 50); // set the height of the SelectPicPopupWindow pop-up form this. setHeight (LayoutParams. WRAP_CONTENT); // you can click this to set the SelectPicPopupWindow pop-up window. setFocusable (true); this. setOutsideTouchable (true); // refresh the status this. update (); // instantiate a ColorDrawable color to translucent ColorDrawable dw = new ColorDrawable (0000000000); // point the back key and other places to make it disappear. If this is set, OnDismisslistener can be triggered, set other controls and perform other operations such as this. setBackgroundDrawable (dw); // mPopupWindow. setAnimationStyle (android. r. style. Animation_Dialog); // set SelectPicPopupWindow to bring up the form animation effect this. setAnimationStyle (R. style. AnimationPreview);} public void showPopupWindow (View parent) {if (! This. isShowing () {this. showAsDropDown (parent, parent. getLayoutParams (). width/2, 18) ;}else {this. dismiss ();}}}
The above uses an animated style effect:/14_CustomPopupWindow/res/values/styles. xml
Two animation resources are used:/14_CustomPopupWindow/res/anim/fade_in.xml.
/14_CustomPopupWindow/res/anim/fade_out.xml
/14_CustomPopupWindow/src/com/wwj/popupwindow/MainActivity. java
package com.wwj.popupwindow;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{private Button setButton;private Button addButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_swipe);setButton = (Button) findViewById(R.id.btnSet);addButton = (Button) findViewById(R.id.btnAdd);setButton.setOnClickListener(this);addButton.setOnClickListener(this);;}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btnSet:MorePopWindow morePopWindow = new MorePopWindow(MainActivity.this);morePopWindow.showPopupWindow(setButton);break;case R.id.btnSearch:break;case R.id.btnAdd:AddPopWindow addPopWindow = new AddPopWindow(MainActivity.this);addPopWindow.showPopupWindow(addButton);break;default:break;}}}
The above is code implementation. For details, refer to the download source code.