Android知识Android技术知识安卓资源收集

Android——PopUpWindow

2017-08-25  本文已影响0人  蔡小树

1.先定义两个PopupWindow进入退出的动画

<!-- push_bottom_in.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="100"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>
</set>
<!-- push_bottom_out.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="100"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>
</set>

2.style文件中创建

<!-- PopWindow的展示与消息 -->
    <style name="mypopwindow_anim_style">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <!-- 指定显示的动画xml -->
        <item name="android:windowExitAnimation">@anim/push_buttom_out</item>
        <!-- 指定消失的动画xml -->
    </style>

3.layout中创建PopupWindow的样式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >
    <LinearLayout
        android:id="@+id/id_exit_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#ffffff"
            android:text="确认退出?"
            android:gravity="center"
            android:padding="5dp"/>
        <Button
            android:id="@+id/but_exit_sure"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#ffffff"
            android:text="确定"
            android:textColor="#ff0000" />
        <Button
            android:id="@+id/but_exit_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="#ffffff"
            android:text="取消" />
    </LinearLayout>
</RelativeLayout>

4.定义个自定义类继承PopupWindow

public class ExitPopupWindow extends PopupWindow implements OnClickListener {
    private View mPopupWindow;
    private OnItemClickListener mListener;
    public ExitPopupWindow(Context context) {
        super(context);
        initPop(context);
        setPopupWindow();
    }
    private void initPop(Context context) {
        // TODO Auto-generated method stub
        mPopupWindow = LayoutInflater.from(context).inflate(R.layout.exit_popup, null);
        Button butExitSure = (Button) mPopupWindow.findViewById(R.id.but_exit_sure);
        Button butExitCancel = (Button) mPopupWindow.findViewById(R.id.but_exit_cancel);
        butExitSure.setOnClickListener(this);
        butExitCancel.setOnClickListener(this);
    }
    /**
     * 设置PopupWindow
     */
    private void setPopupWindow() {
        this.setContentView(mPopupWindow);// 设置View
        this.setWidth(LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.mypopwindow_anim_style);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x33000000));// 设置背景透明
        mPopupWindow.setOnTouchListener(new OnTouchListener() {// 如果触摸位置在窗口外面则销毁
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mPopupWindow.findViewById(R.id.id_exit_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
    /**
     * 定义一个接口,公布出去 在Activity中操作按钮的单击事件
     */
    public interface OnItemClickListener {
        void setOnItemClick(View v);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (mListener != null) {
            mListener.setOnItemClick(v);
        }
    }
}

5.在要使用的activity继承接口OnItemClickListener

mPop = new ExitPopupWindow(SettingActivity.this);
        mPop.setOnItemClickListener(SettingActivity.this);
        //setting 是要弹出PopupWindow的界面的父布局(就是最外层的布局)
        mPop.showAtLocation(SettingActivity.this.findViewById(R.id.setting), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0, 0);
@Override
    public void setOnItemClick(View v) {
       // TODO Auto-generated method stub
       switch (v.getId()) {
       case R.id.but_exit_sure:
           //搞事情
           break;
       case R.id.but_exit_sure:
           mPop.dismiss();
           break;
       default:
           break;
       }
    }
hh.png
上一篇 下一篇

猜你喜欢

热点阅读