这最近刚好写了一个点击分享,弹窗,然后点击各个按钮可以实现点击事件,下面就来分享一下.
弹窗的布局文件,有报错就是图片文件了,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl_profile_share_mask"
android:background="@color/mask_50">
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_profile_video_share_wechat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/res_btn_wx_pressed" />
<ImageView
android:id="@+id/iv_profile_video_share_wechatfriend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/res_btn_share_friends" />
<ImageView
android:id="@+id/iv_profile_video_share_qq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/res_btn_qq_pressed" />
</LinearLayout>
</RelativeLayout>
调用弹窗的方法
//分享框弹出
public void showPopupwindow(){
View inflate = LayoutInflater.from(getContext()).inflate(R.layout.item_profile_video_share_popupwindow, null);
popupWindow = new PopupWindow(inflate, RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//设置背景,这个没什么效果,不添加会报错
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//设置点击弹窗外隐藏自身
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
//设置位置
popupWindow.showAtLocation(inflate, Gravity.BOTTOM, 0, 0);
//设置PopupWindow的View点击事件
setOnPopupViewClick(inflate);
}
//分享点击事件
private void setOnPopupViewClick(View view) {
RelativeLayout rlShareMask = view.findViewById(R.id.rl_profile_share_mask);
ImageView ivShareWechat = view.findViewById(R.id.iv_profile_video_share_wechat);
ImageView ivShareWechatFriend = view.findViewById(R.id.iv_profile_video_share_wechatfriend);
ImageView ivShareQq = view.findViewById(R.id.iv_profile_video_share_qq);
rlShareMask.setOnClickListener(new View.OnClickListener() {//外部透明层
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
ivShareWechat.setOnClickListener(new View.OnClickListener() {//微信
@Override
public void onClick(View v) {
ToastUtil.showCenterToast("微信");
popupWindow.dismiss();
}
});
ivShareWechatFriend.setOnClickListener(new View.OnClickListener() {//朋友圈
@Override
public void onClick(View v) {
ToastUtil.showCenterToast("微信朋友圈");
popupWindow.dismiss();
}
});
ivShareQq.setOnClickListener(new View.OnClickListener() {//QQ
@Override
public void onClick(View v) {
ToastUtil.showCenterToast("QQ");
popupWindow.dismiss();
}
});
}