標籤:
My Phone管家(19) 應用管理 單獨介紹一下PopupWindow
在前一節中使用點擊ListView 出現一個快顯視窗, 用來顯示我們的需求。
PopupWindow: A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.
PopupWindow 是一個彈出表單, 可以顯示任意的視圖, 他是一個浮動的容器, 顯示在當前activity的上方。
注意要給PopupWindow設定一個背景,否則,會出現點擊back鍵和點擊PopupWindow地區外, PopupWindow不會消失,
設定背景:mPopupWindow.setBackgroundDrawable(new ColorDrawable());
(1)按鈕點擊觸發 PopupWindow的顯示,
mPopupWindow.showAsDropDown(pView,60,-pView.getHeight()); 顯示PopupWindow的位置
設定popView的彈出效果動畫,
(2) 當點擊PopupWindow地區以外的地方, PopupWindow消失
(3)點擊PopupWindow視圖的選項,執行相應的操作。
package com.example.testpopmenu; import android.app.ActionBar.LayoutParams;import android.app.Activity;import android.graphics.drawable.ColorDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.ScaleAnimation;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener { PopupWindow mPopupWindow; View popupView; AnimationSet animationset; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * * @param v */ public void Click(View v){ showPopupWindow(v); } @Override public void onClick(View v) { if(mPopupWindow!= null){ mPopupWindow.dismiss();//消失 } switch (v.getId()) { case R.id.tv_lanuch://啟動 Toast.makeText(this, "啟動", Toast.LENGTH_LONG).show(); break; case R.id.tv_uninstall://卸載 Toast.makeText(this, "系統應用,需要root許可權才可以卸載", Toast.LENGTH_LONG).show(); break; case R.id.tv_share://分享 Toast.makeText(this, "分享", Toast.LENGTH_LONG).show(); break; default: break; } } /** *快顯視窗 */ protected void showPopupWindow(View pView){ if(mPopupWindow==null){ popupView = View.inflate(this, R.layout.popup_adapter, null); mPopupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true); //設定一個背景,這樣的話點擊返回鍵可用消失 mPopupWindow.setBackgroundDrawable(new ColorDrawable()); TextView tvStart = (TextView) popupView.findViewById(R.id.tv_lanuch); TextView tvUninstall = (TextView) popupView.findViewById(R.id.tv_uninstall); TextView tvShare=(TextView) popupView.findViewById(R.id.tv_uninstall); tvStart.setOnClickListener(this); tvUninstall.setOnClickListener(this); tvShare.setOnClickListener(this); //縮放動畫 ScaleAnimation a= new ScaleAnimation(0, 1,0,1, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f ); a.setDuration(200); //漸層動畫 AlphaAnimation alpha = new AlphaAnimation(0, 1); alpha.setDuration(200); animationset = new AnimationSet(false); animationset.addAnimation(a); animationset.addAnimation(alpha); } mPopupWindow.showAsDropDown(pView,60,-pView.getHeight()); popupView.startAnimation(animationset); } }
My Phone管家(19) 應用管理 單獨介紹一下PopupWindow