转载请指明出处 :http://blog.youkuaiyun.com/hellohhj/article/details/37600469
主要介绍一下popupWindow的简单用法,包括在控件的上方、正上方、下方、正下方、左方、正左方、右方、正右方以及在屏幕中间显示等等。。。
希望对大家有帮助
主要代码如下:
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("我最亲爱的。。。。。我最亲爱的。。。。。我最亲爱的。。。。。我最亲爱的。。。。。我最亲爱的。。。。。我最亲爱的。。。。。我最亲爱的。。。。。我最亲爱的。。。。。");
tv.setTextColor(Color.WHITE);
layout.addView(tv);
popupWindow = new PopupWindow(layout, 250, 200);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
int v_with = v.getWidth();
int v_height = v.getHeight();
// 屏幕中央
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
;
// // 上方
// popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0],
// location[1] - popupWindow.getHeight());
// // 正上方
// popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
// location[0] + Math.abs((v_with - popupWindow.getWidth()) / 2),
// location[1] - popupWindow.getHeight());
// // 下方
// popupWindow.showAsDropDown(v);
// // 正下方
// popupWindow.showAsDropDown(v,
// Math.abs((v_with - popupWindow.getWidth()) / 2), 0);
//
// // 左方
// popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]
// - popupWindow.getWidth(), location[1]);
// //正左方
// popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]
// - popupWindow.getWidth(), location[1]-Math.abs((v_height -
// popupWindow.getHeight()) / 2));
// // 右方
// popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
// location[0] + v.getWidth(), location[1]);
// 正右方
// popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
// location[0] + v.getWidth(), location[1]-Math.abs((v_height -
// popupWindow.getHeight()) / 2));
}
下面附上源码地址 : http://download.youkuaiyun.com/detail/hellohhj/7612275
5683



被折叠的 条评论
为什么被折叠?



