Android:仿微信表情预览,popwindow的弹框位置

 private void showWinddowAbove(View view,int dra){
        View contentView = LayoutInflater.from(getActivity()).inflate(
                R.layout.poplay, null);
        if(popupWindow == null){
            popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);
            // 如果不设置PopupWindow的背景,有些版本就会出现一个问题:无论是点击外部区域还是Back键都无法dismiss弹框
            popupWindow.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.circle_bg));
            face_img = contentView.findViewById(R.id.face_img);
        }

        Glide.with(getActivity()).load(dra).into(face_img);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
         popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1] - popupWindow.g
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android PopupWindow 实现,类似于微信的弹出菜单: 1. 首先,在你的布局文件中添加一个按钮或者其他可点击的视图: ```xml <Button android:id="@+id/btn_popup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹出菜单" /> ``` 2. 在你的 Activity 或者 Fragment 中,初始化 PopupWindow 对象和布局: ```java private PopupWindow popupWindow; private View popupView; private void initPopupWindow() { // 初始化 PopupWindow 布局 popupView = getLayoutInflater().inflate(R.layout.popup_window, null); // 初始化 PopupWindow 对象 popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); } ``` 3. 给按钮添加点击事件,弹出 PopupWindow: ```java Button btnPopup = findViewById(R.id.btn_popup); btnPopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 显示 PopupWindow popupWindow.showAsDropDown(v); } }); ``` 4. 在 PopupWindow 布局中添加菜单项: ```xml <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="菜单项1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="菜单项2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="菜单项3" /> </LinearLayout> ``` 这样就实现了一个简单的 Android PopupWindow,类似于微信的弹出菜单。你可以根据自己的需求,修改布局和样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值