Android 自定义PopWindow完整代码

1.布局文件的编写

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/comment_item_linear"
android:layout_width="380dp"
android:layout_height="80dp"
android:background="@drawable/comment_popu_window"
android:gravity="center">

<TextView
android:id="@+id/tv_comment_share"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@mipmap/icon_comment_share"
android:gravity="center"
android:paddingBottom="20dp"
android:paddingTop="15dp"
android:text="分享"
android:textColor="@color/white" />

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginBottom="20dp"
android:layout_marginTop="15dp"
android:background="@color/white" />

<TextView
android:id="@+id/tv_comment_pinlun"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@mipmap/icon_comment_pinlun"
android:gravity="center"
android:paddingBottom="20dp"
android:paddingTop="15dp"
android:text="回复"
android:textColor="@color/white" />

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginBottom="20dp"
android:layout_marginTop="15dp"
android:background="@color/white" />

<TextView
android:id="@+id/tv_comment_copy"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@mipmap/icon_comment_copy"
android:gravity="center"
android:paddingBottom="20dp"
android:paddingTop="15dp"
android:text="复制"
android:textColor="@color/white" />

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginBottom="20dp"
android:layout_marginTop="15dp"
android:background="@color/white" />

<TextView
android:id="@+id/tv_comment_report"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@mipmap/icon_comment_report"
android:gravity="center"
android:paddingBottom="@dimen/dp_20"
android:paddingTop="15dp"
android:text="举报"
android:textColor="@color/white" />
</LinearLayout>

2.自定义PopWindow代码部分
package com.bookuu.bookuulibrary.ui.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bookuu.bookuulibrary.R;
import com.bookuu.bookuulibrary.config.MyApp;

/**
* Created by kevin on 2017/12/26.
* <p>
* 复制/回复/评论/举报功能
*/

public class CommtentPopWindow extends PopupWindow implements View.OnClickListener {
private View conentView;
private LinearLayout comment_item_linear;
private TextView tv_comment_share;
private TextView tv_comment_pinlun;
private TextView tv_comment_copy;
private TextView tv_comment_report;
View mPopView;

private OnItemClickListener mListener;

public CommtentPopWindow(final Context context) {
super(context);

initView(context);
setPopupWindow();
tv_comment_share.setOnClickListener(this);
tv_comment_pinlun.setOnClickListener(this);
tv_comment_share.setOnClickListener(this);
tv_comment_copy.setOnClickListener(this);
tv_comment_report.setOnClickListener(this);
}

private void initView(Context context) {

LayoutInflater inflater = LayoutInflater.from(context);
mPopView = inflater.inflate(R.layout.popurwindow_item_comment, null);

comment_item_linear = (LinearLayout) mPopView.findViewById(R.id.comment_item_linear);
tv_comment_share = (TextView) mPopView.findViewById(R.id.tv_comment_share);
tv_comment_pinlun = (TextView) mPopView.findViewById(R.id.tv_comment_pinlun);
tv_comment_copy = (TextView) mPopView.findViewById(R.id.tv_comment_copy);
tv_comment_report = (TextView) mPopView.findViewById(R.id.tv_comment_report);

comment_item_linear.setOnClickListener(this);
tv_comment_share.setOnClickListener(this);
tv_comment_copy.setOnClickListener(this);
tv_comment_pinlun.setOnClickListener(this);
tv_comment_report.setOnClickListener(this);
}

private void setPopupWindow() {
this.setContentView(mPopView);// 设置View
this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
this.setFocusable(true);// 设置弹出窗口可
this.setAnimationStyle(R.style.mypopwindow_anim_style);// 设置动画
this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
// int height = mPopView.findViewById(R.id.id_pop_layout).getTop();
// int y = (int) event.getY();
// if (event.getAction() == MotionEvent.ACTION_UP) {
// if (y < height) {
// dismiss();
// }
// }

MyApp.getApp().showToast("ontouch....");
return true;
}
});
}

/**
* 显示popupWindow
*
* @param parent
*/
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow

// this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
} else {
this.dismiss();
}
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mListener != null) {
mListener.setOnItemClick(v);
}
}


/**
* 定义一个接口,公布出去 在Activity中操作按钮的单击事件
*/
public interface OnItemClickListener {
void setOnItemClick(View v);
}

public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}

}

3.在调用的地方如何使用
mPop = new CommtentPopWindow(context);
mPop.setOnItemClickListener(this);

实现
CommtentPopWindow.OnItemClickListener接口在
setOnItemClick方法中做想做的功能即可

@Override
public void setOnItemClick(View v) {

switch (v.getId()) {
case R.id.comment_item_linear:

// MyApp.getApp().showToast("lin");
break;

case R.id.tv_comment_share:

MyApp.getApp().showToast("分享.....");

break;
case R.id.tv_comment_pinlun:
MyApp.getApp().showToast("评论.....");

break;
case R.id.tv_comment_copy:
// String text;
// ClipboardManager clipboardManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
// clipboardManager.setPrimaryClip(ClipData.newPlainText(text, comment_content));

MyApp.getApp().showToast(" 复制.....");

break;
case R.id.tv_comment_report:

MyApp.getApp().showToast("举报.....");

break;
}
}
原文地址:https://www.cnblogs.com/lenkevin/p/8125277.html