zoukankan      html  css  js  c++  java
  • 在指定控件位置弹出popup window

    先看效果图

    黄色的就是弹出的popup window


    首先自定义一个view用来显示,文件名为layout_my_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_tips"
    android:orientation="vertical">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:textSize="13dp"
    android:textColor="#333333"
    android:text="。。。"/>

    </LinearLayout>

    java 代码实现
    public class TipsView extends LinearLayout {
        public TipsView(Context context) {
            super(context);
            init();
        }
    
        public TipsView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public TipsView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        private void init() {
            inflate(getContext(), R.layout.layout_my_view, this);
        }
    }
    
    

    调用代码

    private void showTips() {
            if (mTipsView == null) {
                mTipsView = new TipsView(this);
                mPopupWindow = new PopupWindow(mTipsView, RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT, true);
                // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
                mPopupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color
                        .transparent));
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);
            }
    
            if (mPopupWindow.isShowing()) {
                return;
            }
    
            // 设置好参数之后再show
            int local[] = new int[2];
            //弹出控件的位置,坐标存在local数组
            mTvBindFlag.getLocationOnScreen(local);
    
            int width = mTipsView.getWidth();
            int height = mTipsView.getHeight();
            if (width == 0 || height == 0) {
                // 获取测量后的宽度
                int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                mTipsView.measure(w, h);
                width = mTipsView.getMeasuredWidth();
                height = mTipsView.getMeasuredHeight();
            }
    
            // x坐标计算方式:complete_count_txt的x坐标加上他的长度一半(相当于complete_count_txt的横向居中位置)
            // ,再减少弹出气泡宽度的一半(相当于向左移动气泡一半的宽度,就居中显示在complete_count_txt了)
            int x = local[0] + (mTvBindFlag.getWidth() / 2) - width / 2;
            // y坐标计算方式:complete_count_txt的y坐标减去气泡的高度
            int y = local[1] - height;
            // 通过绝对位置显示
           @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
         * @param gravity the gravity which controls the placement of the popup window
         * @param x the popup's x location offset
         * @param y the popup's y location offset
         */
         // public void showAtLocation(View parent, int gravity, int x, int y) 
            mPopupWindow.showAtLocation(mTvBindFlag, Gravity.NO_GRAVITY, x, y);
        }
     
  • 相关阅读:
    微信公众平台开发学习系列(四):微信分享内容修改与分享之后才能访问某页面
    微信公众平台开发学习系列(三):网页授权获取用户基本信息
    微信公众平台开发学习系列(二):微信公众平台接收消息与发送消息
    微信公众平台开发学习系列(一):公众平台测试号申请与自定义菜单创建
    EF结合SqlBulkCopy在项目中的使用
    用python实现【五猴分桃】问题
    基于pandas进行数据预处理
    【转载】各类排序算法的对比及实现
    牛客网2017校招真题在线编程之合唱团问题——动态规划问题首秀
    动态规划(DP)算法
  • 原文地址:https://www.cnblogs.com/lovemo1314/p/5487046.html
Copyright © 2011-2022 走看看