Android-自訂PopupWindow

來源:互聯網
上載者:User

Android-自訂PopupWindow
2014年5月12日PopupWindow在應用中應該是隨處可見的,很常用到,比如在舊版本的當中就用到下拉的PopupWindow,那是自訂的。新版5.2的ActionBar,有人已經模仿了它,但具體是使用了ActionBar還是其他的筆者倒是不太清楚,本篇部落客要介紹如何自訂一個PopupWindow來供自己在開發應用時使用。因為筆者最近在開發一款應用時用到這個知識點,所以自己實現了類似新版的效果。源碼下載:http://download.csdn.net/detail/wwj_748/7337175如下:



首先從布局開始/14_CustomPopupWindow/res/layout/activity_swipe.xml

                                                                                                    

/14_CustomPopupWindow/res/layout/add_popup_dialog.xml
                                                                                                



/14_CustomPopupWindow/res/layout/more_popup_dialog.xml
                                                                                                                                                                                                                                                                                                                                            

以上分別是首頁面和兩個popupWindow布局
下面自訂兩個PopupWindow,自己封裝自己想要的PopuoWindow,這裡只是給出樣本

/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;/** * 自訂popupWindow *  * @author wwj *  *  */public class 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().getDefaultDisplay().getHeight();int w = context.getWindowManager().getDefaultDisplay().getWidth();// 設定SelectPicPopupWindow的Viewthis.setContentView(conentView);// 設定SelectPicPopupWindow彈出表單的寬this.setWidth(w / 2 + 50);// 設定SelectPicPopupWindow彈出表單的高this.setHeight(LayoutParams.WRAP_CONTENT);// 設定SelectPicPopupWindow彈出表單可點擊this.setFocusable(true);this.setOutsideTouchable(true);// 重新整理狀態this.update();// 執行個體化一個ColorDrawable顏色為半透明ColorDrawable dw = new ColorDrawable(0000000000);// 點back鍵和其他地方使其消失,設定了這個才能觸發OnDismisslistener ,設定其他控制項變化等操作this.setBackgroundDrawable(dw);// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);// 設定SelectPicPopupWindow彈出表單動畫效果this.setAnimationStyle(R.style.AnimationPreview);LinearLayout addTaskLayout = (LinearLayout) conentView.findViewById(R.id.add_task_layout);LinearLayout teamMemberLayout = (LinearLayout) conentView.findViewById(R.id.team_member_layout);addTaskLayout.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {AddPopWindow.this.dismiss();}});teamMemberLayout.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AddPopWindow.this.dismiss();}});}/** * 顯示popupWindow *  * @param parent */public void showPopupWindow(View parent) {if (!this.isShowing()) {// 以下拉方式顯示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) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);conentView = inflater.inflate(R.layout.more_popup_dialog, null);int h = context.getWindowManager().getDefaultDisplay().getHeight();int w = context.getWindowManager().getDefaultDisplay().getWidth();// 設定SelectPicPopupWindow的Viewthis.setContentView(conentView);// 設定SelectPicPopupWindow彈出表單的寬this.setWidth(w / 2 + 50);// 設定SelectPicPopupWindow彈出表單的高this.setHeight(LayoutParams.WRAP_CONTENT);// 設定SelectPicPopupWindow彈出表單可點擊this.setFocusable(true);this.setOutsideTouchable(true);// 重新整理狀態this.update();// 執行個體化一個ColorDrawable顏色為半透明ColorDrawable dw = new ColorDrawable(0000000000);// 點back鍵和其他地方使其消失,設定了這個才能觸發OnDismisslistener ,設定其他控制項變化等操作this.setBackgroundDrawable(dw);// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);// 設定SelectPicPopupWindow彈出表單動畫效果this.setAnimationStyle(R.style.AnimationPreview);}public void showPopupWindow(View parent) {if (!this.isShowing()) {this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);} else {this.dismiss();}}}


上面用到一個動畫樣式效果:/14_CustomPopupWindow/res/values/styles.xml
 

用到兩個動畫資源/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;}}}

以上是代碼實現,具體可以下載源碼參考。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.