PopUpWindow基本使用

原文地址:PopUpWindow使用詳解(一)——基本使用

個人記錄:

 // pupWindow的layout
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_pop, null, false);
                // 新建popWindow:一參:layout,二參:寬度,三參:高度
                popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setAnimationStyle(R.style.pupDialog); //設置樣式(進出場動畫)
                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000"))); //設置背景顏色(可以作爲半透明大背景)
                popupWindow.setTouchable(true); //設置是否響應觸摸事件
                popupWindow.setFocusable(false);//設置是否能獲取焦點(如果控件中藥EditText需要設置爲true不然無法輸入)
                popupWindow.setOutsideTouchable(true);//設置是否點擊空白取消popWindow
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    popupWindow.setElevation(20f); //設置popWindow高度(達到陰影效果)
                }
                // pupWindow所在的父佈局
                View viewParent = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null, false);
                // 設置pupWindow在父佈局中的位置 一參:父佈局,二參:父佈局中的位置,三參四參:偏移量(可以爲負值,負值向反方向移動)
                popupWindow.showAtLocation(viewParent, Gravity.CENTER, 0, 0);
                // 設置pupWindow相對於某個控件的位置 一參:錨點控件,三參四參:偏移量(可以爲負值,負值向反方向移動)
                popupWindow.showAsDropDown(btn_main, 0, 0);

進出場動畫樣式:

 <!--pupDialog Style-->
    <style name="pupDialog">
        <item name="android:windowEnterAnimation">@anim/in_view</item>
        <item name="android:windowExitAnimation">@anim/out_view</item>
    </style>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章