PopupWindow第一次弹出的位置和第二次不一致

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弹出时就能得到偏移的数值,出现在预期的位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值