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

Android 自定义PopWindow完整代码的更多相关文章

  1. Android 自定义View—清爽小巧灵活的多节点进度条

    前言 最近项目有一个节点进度条的小需求,完成后,想分享出来希望可以帮到有需要的同学. 真机效果图 自定义View完整代码 开箱即用~,注释已经炒鸡详细了 /** * @description: 节点进 ...

  2. 【转】Android自定义Adapter的ListView的思路及代码

    原文网址:http://www.jb51.net/article/37236.htm Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下   在开发中,我们经常使 ...

  3. Android 一个相对完整的自动升级功能实现代码

    由于项目的需要最近做了一个关于Android自动升级的功能,下面将贴出Android手机客户端的完整代码.这段代码参考别的代码居多,由于不满足需求,所以自己仅仅改了一些需要变动的内容,其他功能都是按照 ...

  4. Android 监听双卡信号强度(附完整代码)

    Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...

  5. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  6. [原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  7. Android 自定义title 之Action Bar

    Android 自定义title 之Action Bar 2014-06-29  飞鹰飞龙...  摘自 博客园  阅 10519  转 25 转藏到我的图书馆   微信分享:   Action Ba ...

  8. Android 自定义ScrollView ListView 体验各种纵向滑动的需求

      分类: [android 进阶之路]2014-08-31 12:59 6190人阅读 评论(10) 收藏 举报 Android自定义ScrollView纵向拖动     转载请标明出处:http: ...

  9. android自定义UI模板图文详解

    不知道大家在实际开发中有没有自定义过UI模板?今天花时间研究了一下android中自定义UI模板,与大家分享一下. 每个设计良好的App都是自定义标题栏,在自定义标题栏的过程中大部分人可能都是自定义一 ...

随机推荐

  1. Fluent_Python_Part4面向对象,10-seq-hacking,序列的修改、散列和切片

    第四部分第10章,序列的修改.散列和切片 中文电子书P423 这一章接第1章.第9章,以第9章定义的Vector2d类为基础,定义表示多为向量的Vector类.这个类的行为与Python中标准的不可变 ...

  2. 学好Linux必备知识

    鸟哥的私房菜中提到学好Linux必备的几种技能: 1.  计算器概论不硬件相关知识: 因为既然想要走Linux这门路,信息相关癿基础技能也丌能没有啊! 所以先理觋一下基础癿硬件知识,丌用一定要全懂啦! ...

  3. windows远程linux的方法(不用xshell)

    先cmd进入DOS,再输入命令ssh root@要远程的linux的ip 输入密码 即可进入linux后台.如下图,即为edr后台,可以见到unabackup服务了. 如果是多次远程不同IP,第二次远 ...

  4. 【渗透测试】NSA Windows 0day漏洞+修复方案

    这个漏洞是前段时间爆出来的,几乎影响了全球70%的电脑,不少高校.政府和企业都还在用Windows服务器,这次时间的影响力堪称网络大地震. ------------------------------ ...

  5. Jmeter进行分布式性能测试

    由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运行所谓的 ...

  6. ZOJ4109 Welcome Party(2019浙江省赛)

    并查集算连通块的数量,集合的个数就是必然不开心的人数,再跑bfs,用优先队列维护~ #include<bits/stdc++.h> using namespace std; ; vecto ...

  7. Vue项目引进ElementUI组件

    1.https://blog.csdn.net/Mr_JavaScript/article/details/80741914 1.1 安装 npm install element-ui -save 1 ...

  8. MAC系统 - 基础知识

    一.基础操作 设置:触控板设置 - >学习具体手势 手势:MacBook Pro手势大全必学手势触控板手势有哪些 左键,右键,滑屏,切换到应用... 一指操作: 一指敲击:鼠标左键: 一指按下: ...

  9. Android、iOS与Servlet接口上传文件和JSON串的交互

    package etcom.servlet; import java.io.File; import java.io.IOException; import java.sql.Connection; ...

  10. query_phase_execution_exception

    ES报错信息: { "error": { "root_cause": [ { "type": "query_phase_execu ...