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


    PopupWindow第一次弹出的位置和第二次不一致,第二次才是预期的弹出位置。

    1. private void showTipsPop(View view) {
    2. if (null == textView) {
    3. textView = new TextView(this);
    4. textView.setPadding(15, 20, 15, 20);
    5. textView.setBackgroundResource(getResId("R.drawable.aaaaaaa"));
    6. textView.setLineSpacing(5, 1);
    7. textView.setTextColor(Color.parseColor("#808080"));
    8. textView.setTextSize(13);
    9. }
    10. String content = getResources().getString(getResId("R.string.sssss"));
    11. textView.setText(content);
    12. if (null == mPopupWindow) {
    13. mPopupWindow = new PopupWindow(textView, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,false);
    14. }
    15. mPopupWindow.setOutsideTouchable(true);//是否接收窗口外部触摸事件
    16. mPopupWindow.setAnimationStyle(getResId("R.style.PopupAnimation"));
    17. int x = view.getMeasuredWidth() + 20;
    18. int y = -textView.getMeasuredHeight() / 2 - view.getMeasuredHeight() / 2;
    19. mPopupWindow.showAsDropDown(view, x, y);
    20. mPopupWindow.update();
    21. }

    这个时候,第一次弹出时,由于view这个控件和textview这2个控件没有进行绘制,所以,它们俩的宽高是0,导致PopupWindow弹出位置没有在预期的位置出现,解决方法是在view弹出前,让这两个控件强行绘制,如下:

    1. private void showTipsPop(View view) {
    2. if (null == textView) {
    3. textView = new TextView(this);
    4. textView.setPadding(15, 20, 15, 20);
    5. textView.setBackgroundResource(getResId("R.drawable.aaaa"));
    6. textView.setLineSpacing(5, 1);
    7. textView.setTextColor(Color.parseColor("#808080"));
    8. textView.setTextSize(13);
    9. }
    10. String content = getResources().getString(getResId("R.string.sss"));
    11. textView.setText(content);
    12. if (null == mPopupWindow) {
    13. mPopupWindow = new PopupWindow(textView, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,false);
    14. }
    15. mPopupWindow.setOutsideTouchable(true);//是否接收窗口外部触摸事件
    16. mPopupWindow.setAnimationStyle(getResId("R.style.PopupAnimation"));
    17. view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    18. textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    19. int x = view.getMeasuredWidth() + 20;
    20. int y = -textView.getMeasuredHeight() / 2 - view.getMeasuredHeight() / 2;
    21. mPopupWindow.showAsDropDown(view, x, y);
    22. mPopupWindow.update();
    23. }

    绘制代码:

    1. view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    2. textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

    这个时候,PopupWindow弹出时就能得到偏移的数值,出现在预期的位置。

  • 相关阅读:
    dpdk-lcore-mask 对 handler线程cpu亲和性的影响
    c/c++: window下安装mingw-w64
    Spring Boot自动配置
    leetcode 2. 两数相加
    Linux学习(4)——Linux目录结构
    【EMC专题】常见的电磁骚扰源——人为骚扰源的分类
    Mac使用unrar和rar解压文件
    解决:在单项目组件里面引入 base.scss/ base.less 等的外部文件不成功的问题
    推荐一个基于.NET Core 3.1开发开源的分布式任务调度系统
    Dubbo—dubbo admin安装
  • 原文地址:https://blog.csdn.net/yuzhidao/article/details/128003619