1. 程式人生 > >【每天積累一點點】將PopupWindow顯示在指定位置

【每天積累一點點】將PopupWindow顯示在指定位置

//以想在控制元件tvMl的右下角顯示PopupWindow為例(popupWindow的右上角在tvMl的右下角)
            View popPhotoView = LayoutInflater.from(this).inflate(R.layout.popup_ml_choose, null);
            mlWindow = new PopupWindow(popPhotoView, Utils.dip2px(this, 132), Utils.dip2px(this, 176), true);
            mlWindow.setBackgroundDrawable(new BitmapDrawable());
            mlWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
            mlWindow.setOutsideTouchable(true);
            //獲取tvMl在螢幕中的位置,其中location[0]是tvMl左上角的x位置,location[1]是tvMl左上角y軸的位置
            int[] location = new int[2];
            tvMl.getLocationOnScreen(location);
//            showAtLocation(View parent, int gravity, int x, int y),其中x,y引數是PopupWindow的左上角的(x,y)座標
            mlWindow.showAtLocation(tvMl, Gravity.NO_GRAVITY, location[0] + tvMl.getWidth() - mlWindow.getWidth(), location[1] + tvMl.getHeight());