Android——PopUpWindow

1.先定義兩個(gè)PopupWindow進(jìn)入退出的動(dòng)畫

<!-- 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文件中創(chuàng)建

<!-- PopWindow的展示與消息 -->
    <style name="mypopwindow_anim_style">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <!-- 指定顯示的動(dòng)畫xml -->
        <item name="android:windowExitAnimation">@anim/push_buttom_out</item>
        <!-- 指定消失的動(dòng)畫xml -->
    </style>

3.layout中創(chuàng)建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="確認(rèn)退出?"
            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.定義個(gè)自定義類繼承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);
    }
    /**
     * 設(shè)置PopupWindow
     */
    private void setPopupWindow() {
        this.setContentView(mPopupWindow);// 設(shè)置View
        this.setWidth(LayoutParams.MATCH_PARENT);// 設(shè)置彈出窗口的寬
        this.setHeight(LayoutParams.WRAP_CONTENT);// 設(shè)置彈出窗口的高
        this.setFocusable(true);// 設(shè)置彈出窗口可
        this.setAnimationStyle(R.style.mypopwindow_anim_style);// 設(shè)置動(dòng)畫
        this.setBackgroundDrawable(new ColorDrawable(0x33000000));// 設(shè)置背景透明
        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;
            }
        });
    }
    /**
     * 定義一個(gè)接口,公布出去 在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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評(píng)論 25 709
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,336評(píng)論 0 17
  • PopupWindow是Android上自定義彈出窗口,使用起來(lái)很方便。 PopupWindow的構(gòu)造函數(shù)為 pu...
    OzanShareing閱讀 840評(píng)論 1 2
  • 1.問題背景 錯(cuò)誤日志如下: 產(chǎn)生背景:在PopupWindow中使用Spinner且SpinnerMode="d...
    紫豪閱讀 4,059評(píng)論 0 7
  • “前不漏縫,后不漏洞,雙腳分開約60度……”教官略微痞氣的說(shuō)道。 “噗嗤”江念笙忍不住笑了,其實(shí)這也真的不能怪她,...
    縛束縛閱讀 374評(píng)論 0 3

友情鏈接更多精彩內(nèi)容