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

  • 相关阅读:
    如何在Linux环境搭建SVN服务器并实现公网访问
    梳理一名Go后端程序员日常用的软件~
    代码随想录算法训练营第二十三天|669 修剪二叉搜索树 108 将有序数组转换为二叉搜索树 538 把二叉搜索树转换为累加树
    uniapp小程序无缝自动滚动 一屏多张图效果demo(整理)
    如何在OrangePi AIpro智能小车上实现安全强化学习算法
    JS-对象 - 1
    易动纷享--测试实习生视频面试
    ClickHouse介绍和使用
    Spark调度核心组件之三剑客
    java毕业设计峨眉山景点介绍及旅游攻略推荐平台Mybatis+系统+数据库+调试部署
  • 原文地址:https://blog.csdn.net/yuzhidao/article/details/128003619