Android PopupWindow 简单用法

可上下移动的TextView,左侧是一个PopupWindow跟随其移动

在这里插入图片描述

DragTextView.java

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastTouchedY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int distanceY = (int) (event.getRawY() - lastTouchedY);
                if (distanceY != 0) {
                    FrameLayout.MarginLayoutParams curParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
                    int topMargin = (curParams.topMargin + distanceY);
                    if (topMargin + getHeight() > ((ViewGroup) getParent()).getHeight()) {
                        topMargin = ((ViewGroup) getParent()).getHeight() - getHeight();
                    }
                    if (topMargin < 0) {
                        topMargin = 0;
                    }
                    curParams.topMargin = topMargin;
                    setLayoutParams(curParams);
                    lastTouchedY = event.getRawY();
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }


    private HideSubtitlePopupWindow popupWindow;

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d(TAG, "onLayout: " + changed + "---left=" + left + "---top=" + top + "---right=" + right + "---bottom=" + bottom);
        //
        int popWidth = SizeUtils.dp2px(30);
        int popHeight = SizeUtils.dp2px(30);
        int popLeft = -popWidth;
        int popTop = -popHeight - (getHeight() - popHeight) / 2;
        Log.d(TAG, "onLayout: popLeft=" + popLeft + "---popTop=" + popTop);
        if (popupWindow == null) {
            popupWindow = new HideSubtitlePopupWindow(getContext(), popWidth, popHeight);
            popupWindow.showAtLocation(this, Gravity.NO_GRAVITY, popLeft, popTop);
        }
        popupWindow.update(this, popLeft, popTop, -1, -1);
    }

HideSubtitlePopupWindow.java

public class HideSubtitlePopupWindow extends PopupWindow {

    public HideSubtitlePopupWindow(Context context, int width, int height) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View mView = inflater.inflate(R.layout.hide_subtitle_popup_view, null);

        this.setContentView(mView);
        this.setWidth(width);
        this.setHeight(height);
        this.setAnimationStyle(R.style.PopScaleAnimation);
        this.setFocusable(false);
        this.setOutsideTouchable(false);

        TextView textView = mView.findViewById(R.id.tvText);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick();
                }
            }
        });
    }

    public interface OnClickListener {
        void onClick();
    }

    private OnClickListener listener;

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }
}

Kotlin中使用的基本模板

class PopupCouponTipsEditorTop(context: Context) : PopupWindow(context) {

    private val binding: StorePopupCouponTipsEditorTopBinding =
            DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.store_popup_coupon_tips_editor_top, null, false)

    init {
        contentView = binding.root
        isFocusable = false
        isOutsideTouchable = false
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        initView()
    }

    private fun initView() {
        binding.ivClose.setOnClickListener { dismiss() }
        binding.llContent.setOnClickListener {
            //todo 领取或使用
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值