Android PopupWindow设置

来源:互联网 发布:网络综艺节目评价 编辑:程序博客网 时间:2024/06/02 05:32
package com.example.picpopupwindow;import android.app.Activity;import android.content.Context;import android.graphics.drawable.ColorDrawable;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.PopupWindow;public class MyPopupWindow extends PopupWindow {private Button btn_cancel;private View mMenuView;public MyPopupWindow(Activity context) {super(context);LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);mMenuView = inflater.inflate(R.layout.alert_dialog, null);btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);btn_cancel.setOnClickListener(new OnClickListener() {public void onClick(View v) {// 销毁弹出框dismiss();}});// 设置SelectPicPopupWindow的Viewthis.setContentView(mMenuView);// 设置SelectPicPopupWindow弹出窗体的宽this.setWidth(LayoutParams.FILL_PARENT);// 设置SelectPicPopupWindow弹出窗体的高this.setHeight(LayoutParams.WRAP_CONTENT);// 设置SelectPicPopupWindow弹出窗体可点击this.setFocusable(true);// 设置SelectPicPopupWindow弹出窗体动画效果this.setAnimationStyle(R.style.AnimBottom);// 实例化一个ColorDrawable颜色为半透明ColorDrawable dw = new ColorDrawable(0xb0000000);// 设置SelectPicPopupWindow弹出窗体的背景this.setBackgroundDrawable(dw);// 设置点击其它消失this.setOutsideTouchable(true);// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框mMenuView.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent event) {int height = mMenuView.findViewById(R.id.pop_layout).getTop();int y = (int) event.getY();if (event.getAction() == MotionEvent.ACTION_UP) {if (y < height) {dismiss();}}return true;}});}}

其中动画:

<style name="AnimBottom>     <item name="android:windowEnterAnimation">@anim/name1</item>       <item name="android:windowExitAnimation">@anim/name2</item>  </style> 



0 0
原创粉丝点击