這篇文章主要介紹了popupWindow 在控件的各個方向上的顯示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:
在控件的上方:
private void showPopUp(View v) {
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("I'm a pop -----------------------------!");
tv.setTextColor(Color.WHITE);
layout.addView(tv);
popupWindow = new PopupWindow(layout,120,120);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
}
在控件的其他方向上顯示只需修改最後一行代碼即可,如:
下方:PopupmenuCopy.showAtLocation(mMoblieTextView,Gravity.NO_GRAVITY, location[0],location[1]+mMoblieTextView.getHeight());
左邊:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
右邊:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);