我试图在用户触摸屏幕的任何地方显示弹出窗口。我能够在所需位置显示弹出窗口。但是问题是如果我在横向模式下将弹出窗口显示在同一位置时在弹出窗口处于纵向模式时出现问题,因为该弹出窗口与横向模式下的视图重叠,并且当我们将方向横向更改为纵向时也会出现相同的问题。我的要求低于1。更改方向时,不希望关闭弹出窗口。2.每当方向发生变化时,请动态更改所有弹出窗口的位置,以使视图不重叠(弹出窗口与图像不重叠)。例如,当我将方向肖像更改为横向弹出窗口时,位置将向上移动。
private void showPopup(final Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;
boolean showEditText = true;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(false);
popup.setOutsideTouchable(false);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 5;
int OFFSET_Y = 5;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
final Button addname = (Button) layout.findViewById(R.id.addName);
}
Run Code Online (Sandbox Code Playgroud)
我知道这不是一个好的解决方案,但我们可以这样做
制作popup为类级别对象
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
showPopup(...);
}
}
},500);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1118 次 |
| 最近记录: |