源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-04-11 10:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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>

效果图: [img]http://files.jb51.net/file_images/article/201706/2017619102206202.jpg?2017519102259[/img] 根据上面可以自行调整位置。 完整实例代码点击此处[url=http://xiazai.jb51.net/201706/yuanma/Android-DemoPopMenu(jb51.net).rar][b]本站下载[/b][/url]。 更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/402.htm]Android数据库操作技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部