Android用Popupwindow做消息長(zhǎng)按提示框(一)

一、popupwindow工具類(lèi)

/**

* @創(chuàng)建者 Sun

* @創(chuàng)建時(shí)間 2016/8/19

* @描述 popupwindow工廠

*/

public class PopupWindowUtils {

/**

* 得到在屏幕中間的popupwindow并顯示(寬高都是包裹視圖)

*

* @param contentView popupwindow要顯示的視圖

* @param parentView? 參考視圖

* @return

*/

public static PopupWindow getPopupWindowInCenter(View contentView, View parentView) {

//? ? ? ? int width = ViewGroup.LayoutParams.MATCH_PARENT;

int width = ViewGroup.LayoutParams.WRAP_CONTENT;

int height = ViewGroup.LayoutParams.WRAP_CONTENT;

return getPopupWindowInCenter(contentView, width, height, parentView);

}

/**

* 得到在屏幕中間的popupwindow并顯示

*

* @param contentView popupwindow要顯示的視圖

* @param width? ? ? popupwindow的寬度

* @param height? ? ? popupwindow的高度

* @param parentView? 參考視圖

* @return

*/

public static PopupWindow getPopupWindowInCenter(View contentView, int width, int height, View parentView) {

//Gravity.CENTER:在屏幕居中,無(wú)偏移

return getPopupWindowAtLocation(contentView, width, height, parentView, Gravity.CENTER, 0, 0);

}

/**

* 得到指定在某個(gè)視圖內(nèi)位置的popupwindow并顯示

*

* @param contentView popupwindow要顯示的視圖

* @param width? ? ? popupwindow的寬度

* @param height? ? ? popupwindow的高度

* @param parentView? 參考視圖

* @param gravityType 在參考視圖中的相對(duì)位置

* @param xoff? ? ? ? x軸偏移量

* @param yoff? ? ? ? y軸偏移量

* @return

*/

public static PopupWindow getPopupWindowAtLocation(View contentView, int width, int height, View parentView, int gravityType, int xoff, int yoff) {

PopupWindow popupWindow = getPopupWindow(contentView, width, height);

//在parentView中偏移xoff和yoff

popupWindow.showAtLocation(parentView,

gravityType, xoff, yoff);

return popupWindow;

}

public static PopupWindow getPopupWindowAtLocation(View contentView, View parentView, int gravityType, int xoff, int yoff) {

return getPopupWindowAtLocation(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, parentView, gravityType, xoff, yoff);

}

/**

* 得到一個(gè)自動(dòng)識(shí)別在目標(biāo)控件上方或下方的pupupwindow并顯示

*

* @param contentView popupwindow要顯示的視圖

* @param width? ? ? popupwindow的寬度

* @param activity? ? 能得到getWindowManager()的上下文

* @return

*/

public static PopupWindow getPopupWindowAsDropDownParentAuto(View contentView, int width, int height, View anchorView, Activity activity) {

//? ? ? ? View itemView = (View) contentView.getParent();// 得到contentView的父控件

PopupWindow popupWindow = getPopupWindow(contentView, width, height);

// 控制它放置的位置

if (isShowBottom(activity, anchorView)) {// 顯示popupwindow在itemView的下方,偏移量都為0

popupWindow.showAsDropDown(anchorView, 0, 0);

} else {// 顯示popupwindow在itemView的上方,偏移量y都為-2*itemView.getHeight()

popupWindow.showAsDropDown(anchorView, 0,

-2 * anchorView.getHeight());

}

return popupWindow;

}

/**

* 得到在指定某個(gè)視圖外的popupwindow并顯示

*

* @param contentView popupwindow要顯示的視圖

* @param width? ? ? popupwindow的寬度

* @param height? ? ? popupwindow的高度

* @param anchorView? 參考視圖

* @param xoff? ? ? ? x軸偏移量

* @param yoff? ? ? ? y軸偏移量

* @return

*/

public static PopupWindow getPopupWindowAsDropDown(View contentView, int width, int height, View anchorView, int xoff, int yoff) {

PopupWindow popupWindow = getPopupWindow(contentView, width, height);

popupWindow.showAsDropDown(anchorView, xoff, yoff);

return popupWindow;

}

/**

* 得到在指定某個(gè)視圖外的popupwindow并顯示(該方法只支持4.4以上)

*

* @param contentView popupwindow要顯示的視圖

* @param width? ? ? popupwindow的寬度

* @param height? ? ? popupwindow的高度

* @param anchorView? 參考視圖

* @param gravityType 在參考視圖外的相對(duì)位置

* @param xoff? ? ? ? x軸偏移量

* @param yoff? ? ? ? y軸偏移量

* @return

*/

@TargetApi(Build.VERSION_CODES.KITKAT)

public static PopupWindow getPopupWindowAsDropDown(View contentView, int width, int height, View anchorView, int gravityType, int xoff, int yoff) {

PopupWindow popupWindow = getPopupWindow(contentView, width, height);

popupWindow.showAsDropDown(anchorView, xoff, yoff, gravityType);

return popupWindow;

}

/**

* 判斷popupWindow是否顯示在條目的下方

*

* @param itemView

* @return

*/

private static boolean isShowBottom(Activity context, View itemView) {

// 得到屏幕的高度

// int heightPixels =

// getResources().getDisplayMetrics().heightPixels;//方式1

int screenHeight = context.getWindowManager().getDefaultDisplay().getHeight();// 方式2

int[] location = new int[2];

// location[0]-->x

// location[1]-->y

itemView.getLocationInWindow(location);

// 得到itemView在屏幕中Y軸的值

int itemViewY = location[1];

// 得到itemView距離屏幕底部的距離

int distance = screenHeight - itemViewY - itemView.getHeight();

if (distance < itemView.getHeight()) {// 條目下方放不下popupWindow

return false;

} else {// 條目下方放得下popupWindow

return true;

}

}

/**

* 最核心的pupupwindow創(chuàng)建方法

*

* @param contentView popupwindow要顯示的視圖

* @param width? ? ? popupwindow的寬度

* @param height? ? ? popupwindow的高度

* @return

*/

@NonNull

private static PopupWindow getPopupWindow(View contentView, int width, int height) {

PopupWindow popupWindow = new PopupWindow(contentView, width, height, true);

popupWindow.setOutsideTouchable(false);

openOutsideTouchable(popupWindow);

return popupWindow;

}

/**

* 點(diǎn)擊popupwindow范圍以外的地方時(shí)隱藏

*

* @param popupWindow

*/

public static void openOutsideTouchable(PopupWindow popupWindow) {

popupWindow.setBackgroundDrawable(new BitmapDrawable());

popupWindow.setOutsideTouchable(true);

}

/**

* 使window變暗

*/

public static void makeWindowDark(Activity activity) {

makeWindowDark(activity, 0.7f);

}

public static void makeWindowDark(Activity activity, float alpha) {

WindowManager.LayoutParams lp = activity.getWindow().getAttributes();

lp.alpha = alpha;

activity.getWindow().setAttributes(lp);

}

/**

* 使window變亮

*/

public static void makeWindowLight(Activity activity) {

WindowManager.LayoutParams lp = activity.getWindow().getAttributes();

lp.alpha = 1f;

activity.getWindow().setAttributes(lp);

}

}

二、在代碼中的使用,以下代碼寫(xiě)在你得OnLongClick點(diǎn)擊事件下面

View menuView = View.inflate(mContext, R.layout.layout_longclick_popwindow, null);

final PopupWindow popupWindow = PopupWindowUtils.getPopupWindowAsDropDownParentAuto(menuView, RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT, view, mContext);

menuView.findViewById(R.id.tvcope).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);

cm.setText(sText);

popupWindow.dismiss();

}

});

menuView.findViewById(R.id.tvforward).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

popupWindow.dismiss();

}

});

menuView.findViewById(R.id.tvcollection).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

popupWindow.dismiss();

}

});

menuView.findViewById(R.id.tvdelete).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

}

});

三、布局代碼

就是在CardView里面創(chuàng)建一個(gè)LinearLayout,下面放你需要的Textview

四、使用起來(lái)還是有一定問(wèn)題

getPopupWindowAsDropDownParentAuto()這個(gè)自動(dòng)識(shí)別在目標(biāo)控件上方或下方的pupupwindow還是有點(diǎn)問(wèn)題,希望各位大佬多多指教

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容