PopupWindow第一次弹出的位置和第二次不一致,第二次才是预期的弹出位置。
- private void showTipsPop(View view) {
- if (null == textView) {
- textView = new TextView(this);
- textView.setPadding(15, 20, 15, 20);
- textView.setBackgroundResource(getResId("R.drawable.aaaaaaa"));
- textView.setLineSpacing(5, 1);
- textView.setTextColor(Color.parseColor("#808080"));
- textView.setTextSize(13);
- }
- String content = getResources().getString(getResId("R.string.sssss"));
- textView.setText(content);
- if (null == mPopupWindow) {
- mPopupWindow = new PopupWindow(textView, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,false);
- }
- mPopupWindow.setOutsideTouchable(true);//是否接收窗口外部触摸事件
- mPopupWindow.setAnimationStyle(getResId("R.style.PopupAnimation"));
-
-
- int x = view.getMeasuredWidth() + 20;
- int y = -textView.getMeasuredHeight() / 2 - view.getMeasuredHeight() / 2;
- mPopupWindow.showAsDropDown(view, x, y);
- mPopupWindow.update();
- }
这个时候,第一次弹出时,由于view这个控件和textview这2个控件没有进行绘制,所以,它们俩的宽高是0,导致PopupWindow弹出位置没有在预期的位置出现,解决方法是在view弹出前,让这两个控件强行绘制,如下:
- private void showTipsPop(View view) {
- if (null == textView) {
- textView = new TextView(this);
- textView.setPadding(15, 20, 15, 20);
- textView.setBackgroundResource(getResId("R.drawable.aaaa"));
- textView.setLineSpacing(5, 1);
- textView.setTextColor(Color.parseColor("#808080"));
- textView.setTextSize(13);
- }
- String content = getResources().getString(getResId("R.string.sss"));
- textView.setText(content);
- if (null == mPopupWindow) {
- mPopupWindow = new PopupWindow(textView, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,false);
- }
- mPopupWindow.setOutsideTouchable(true);//是否接收窗口外部触摸事件
- mPopupWindow.setAnimationStyle(getResId("R.style.PopupAnimation"));
-
- view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
- textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
-
- int x = view.getMeasuredWidth() + 20;
- int y = -textView.getMeasuredHeight() / 2 - view.getMeasuredHeight() / 2;
- mPopupWindow.showAsDropDown(view, x, y);
- mPopupWindow.update();
- }
绘制代码:
- view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
- textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
这个时候,PopupWindow弹出时就能得到偏移的数值,出现在预期的位置。