像微信一样优雅的弹出菜单框

这篇博客展示了如何创建类似微信风格的弹出菜单,包括详细的效果图展示和实现代码,帮助读者理解并实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.先看一下效果图


动图还不会上传,各位凑合着看吧,意思和微信的那个长按应该是一样的尴尬

2.上代码

public class TestActivity extends Activity implements View.OnLongClickListener, View.OnTouchListener {

    private Button mTopMBtn;
    private Button mBottomMBtn;
    private RelativeLayout mRelativeLayout;
    private int mWindowHeight;
    private int mParentHeight;
    private int mViewHeight;
    private int mTitleHeight;
    private int mDialogHeight;
    private int point_width;
    private Activity mActivity;
    private PopupWindow mPopupWindow;
    private View dialogView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_test);
        mActivity = this;
        initView();
        dialogView = LayoutInflater.from(this).inflate(R.layout.location_pop,null,false);
        mPopupWindow = new PopupWindow(dialogView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setOutsideTouchable(true);
        getWindow().getDecorView().addOnAttachStateChangeListener(mOnAttachStateChangeListener);
    }

    private void initView() {
        mTopMBtn = (Button) findViewById(R.id.mBtn_top);
        mBottomMBtn = (Button) findViewById(R.id.mBtn_bottom);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.mRel);
        //OnTouch事件 监听手指的位置 来具体的显示窗口
        mTopMBtn.setOnTouchListener(this);
        mBottomMBtn.setOnTouchListener(this);
        mTopMBtn.setOnLongClickListener(this);
        mBottomMBtn.setOnLongClickListener(this);
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()){
            case R.id.mBtn_top:
                showPop(v);
                break;
            case R.id.mBtn_bottom:
                showPop(v);
                break;
        }
        return true;
    }

    /**
     * 显示pop
     * @param view
     */
    private void showPop(View view){
        if (dealLocation(view)){
            mPopupWindow.showAsDropDown(view,point_width,-(mViewHeight+mDialogHeight));
        }else {
            mPopupWindow.showAsDropDown(view,point_width,0);
        }
    }

    /**
     * 根据控件的位置和弹窗的高度控制弹窗的显示位置
     * true 显示在上边 false 显示在下边
     * @param view
     * @return
     */
    private boolean dealLocation(View view){
        int [] location = new int[2];
        view.getLocationOnScreen(location);
        int height = location[1];
        return height-mTitleHeight+mDialogHeight>mParentHeight;
    }

    /**
     * 获得整体高度
     * @param activity
     * @return
     */
    private int getWindowHeight(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels;
    }

    /**
     * 获得单个view的高度
     *
     * @param view
     * @return
     */
    private int getViewHeight(View view) {
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        return view.getMeasuredHeight();
    }

    /**
     * 获得标题栏和状态栏的高度
     *
     * @param activity
     * @param view     --->父容器
     * @return
     */
    private int getTitleHeight(Activity activity, View view) {
        return getWindowHeight(activity) - getViewHeight(view);
    }

    View.OnAttachStateChangeListener mOnAttachStateChangeListener = new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View v) {
            mWindowHeight = getWindowHeight(mActivity);
            mParentHeight = getViewHeight(mRelativeLayout);
            mViewHeight = getViewHeight(mTopMBtn);
            mTitleHeight = getTitleHeight(mActivity,mRelativeLayout);
            mDialogHeight = getViewHeight(dialogView);
        }

        @Override
        public void onViewDetachedFromWindow(View v) {

        }
    };

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        point_width = (int) event.getX();
        return false;
    }
}
其实很简单,注释写的也挺详细的,理解了就很容易了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值