Android 自定义PopWindow完整代码
阅读原文时间:2023年07月11日阅读:1

1.布局文件的编写


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

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.
*


* 复制/回复/评论/举报功能
*/

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;  
        }  
    }