android 弹窗选项,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

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/pop_bg" >

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" />

效果图:

073257250c2c343118a8ee27705225c6.png

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值