Android长按弹出选项框View进行操作 Android编程实现长按弹出选项框View进行操作的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android长按弹出选项框View进行操作 Android编程实现长按弹出选项框View进行操作的方法

Beyond0525   2021-03-24 我要评论
想了解Android编程实现长按弹出选项框View进行操作的方法的相关内容吗,Beyond0525在本文为您仔细讲解Android长按弹出选项框View进行操作的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,长按,弹出选项框,View,操作,下面大家一起来学习吧。

本文实例讲述了Android编程实现长按弹出选项框View进行操作的方法。分享给大家供大家参考,具体如下:

长按弹出选项框View进行操作

主要代码解释

private void showPopWindows(View v) {
    /** pop view */
    View mPopView = LayoutInflater.from(this).inflate(R.layout.popup, null);
    final PopupWindow mPopWindow = new PopupWindow(mPopView, ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT, true);
    /** set */
    mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    /** 这个很重要 ,获取弹窗的长宽度 */
    mPopView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    int popupWidth = mPopView.getMeasuredWidth();
    int popupHeight = mPopView.getMeasuredHeight();
    /** 获取父控件的位置 */
    int[] location = new int[2];
    v.getLocationOnScreen(location);
    /** 显示位置 */
    mPopWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1]
        - popupHeight);
    mPopWindow.update();
    final String copyTxt = (String) v.getTag();
    mPopView.findViewById(R.id.tv_copy_txt).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        copyToClip(copyTxt);
        if (mPopWindow != null) {
          mPopWindow.dismiss();
        }
      }
    });
}

layout

<?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/pop_bg" >
  <TextView
    android:id="@+id/tv_copy_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:text="复制邀请码"
    android:textColor="@android:color/white"
    android:textSize="12sp" />
</LinearLayout>

效果图:

根据上面可以自行调整位置。

完整实例代码点击此处本站下载

希望本文所述对大家Android程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们