Android自定义PopupWindow小例子

来源:互联网 发布:js数组删除第一个元素 编辑:程序博客网 时间:2024/05/22 05:25
 PopupWindow是我们开发中的常客之一,使用起来也比较简单方便。 写了个最简单地自定义PopupWindow,记录下来,方便以后使用时直接在上面改动就可以。
/** * @param * @author ldm * @description 自定义PopupWindow * @time 2016/9/29 15:26 */public class CustomPopup extends PopupWindow {    //上下文    private Context mContext;    // PopupWindow中控件点击事件回调接口    private IPopuWindowListener mOnClickListener;    //PopupWindow布局文件中的Button    private Button alarm_pop_btn;     /**      * @description 构造方法      * @author ldm      * @time  2016/9/30 9:14      * @param    */    public CustomPopup(Context mContext, int width, int height, IPopuWindowListener listener) {        super(mContext);        this.mContext = mContext;        this.mOnClickListener = listener;        //获取布局文件        View mContentView = LayoutInflater.from(mContext).inflate(R.layout.alarm_disopse_pop, null);        //设置布局        setContentView(mContentView);        // 设置弹窗的宽度和高度        setWidth(width);        setHeight(height);        //设置能否获取到焦点        setFocusable(false);        //设置PopupWindow进入和退出时的动画效果        setAnimationStyle(R.style.popwindow_exit_anim_style);        setTouchable(true); // 默认是true,设置为false,所有touch事件无响应,而被PopupWindow覆盖的Activity部分会响应点击        // 设置弹窗外可点击,此时点击PopupWindow外的范围,Popupwindow不会消失        setOutsideTouchable(false);        //外部是否可以点击,设置Drawable原因可以参考:http://blog.csdn.net/harvic880925/article/details/49278705        setBackgroundDrawable(new BitmapDrawable());        // 设置弹窗的布局界面        initUI();    }    /**     * 初始化弹窗列表     */    private void initUI() {    //获取到按钮        alarm_pop_btn = (Button) getContentView().findViewById(R.id.alarm_pop_btn);        //设置按钮点击事件        alarm_pop_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if (null != mOnClickListener) {                    mOnClickListener.dispose();                }            }        });    }    /**     * 显示弹窗列表界面     */    public void show(View view) {        int[] location = new int[2];        view.getLocationOnScreen(location);        //Gravity.BOTTOM设置在view下方,还可以根据location来设置PopupWindowj显示的位置        showAtLocation(view, Gravity.BOTTOM, 0, 0);    }    /**     * @param     * @author ldm     * @description 点击事件回调处理接口     * @time 2016/7/29 15:30     */    public interface IPopuWindowListener {        void dispose();    }}

注:设置PopupWindow的宽高还可以通过LayoutParams来设置,比如:

 //通过LayoutParams来设置PopupWindow的高度和宽度   ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)mContentView.getLayoutParams();     lp.widht=400;     lp.height = 180;    mContentView.setLayoutParams(lp);    //但是直接这样写获取到的lp可能为空,我们在获取一个View的LayoutParams时,通常应该这样写:    //在addOnGlobalLayoutListener监听中来获取View的LayoutParams    mContentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mContentView.getLayoutParams();                lp.height = 180;                lp.width = 400;                mContentView.setLayoutParams(lp);            }        });

在Activity中使用:

private CustomPopup alarmPopup;....//初始化PopupWindow这里通过数据200来设置PopupWindow高度alarmPopup=new CustomPopup(getActivity(), ViewGroup.LayoutParams.MATCH_PARENT, 200, this);//这里的this是指当前Activity实现了PopupWindow中IPopuWindowListener接口//弹出PopupWindow @Override    protected void widgetClick(View v) {        super.widgetClick(v);        switch (v.getId()) {            case R.id.popup:               alarmPopup.show(v);                break;        }    }    //处理PopupWindow中按钮点击回调     @Override    public void dispose() {       //TODO sth        if (alarmPopup.isShowing()) {                alarmPopup.dismiss();//关闭PopupWindow            }    }

PopupWindow对应 的布局界面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/pop_ll"    android:layout_width="match_parent"    android:layout_height="60dp"    android:background="#404040"    android:orientation="horizontal">    <Button        android:id="@+id/alarm_pop_btn"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@null"        android:gravity="center"        android:text="@string/dispose"        android:textColor="#FFFFFF"        android:textSize="18sp" /></LinearLayout>

动画style:

    <style name="popwindow_exit_anim_style" parent="android:Animation">        <item name="android:windowEnterAnimation">@anim/popshow_anim</item>        <!-- 指定显示的动画xml -->        <item name="android:windowExitAnimation">@anim/pophidden_anim</item>        <!-- 指定消失的动画xml -->    </style>

popshow_anim.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >   <translate    android:duration="500"    android:fromYDelta="100%p"    android:toYDelta="0" />  <alpha    android:duration="500"    android:fromAlpha="0.0"    android:toAlpha="1.0" /></set>

pophidden_anim.xml:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >     <translate    android:duration="500"    android:fromYDelta="0"    android:toYDelta="50%p" />  <alpha    android:duration="500"    android:fromAlpha="1.0"    android:toAlpha="0.0" /></set>
1 1
原创粉丝点击