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. Unix套接字接口

    简介 套接字是操作系统中用于网络通信的重要结构,它是建立在网络体系结构的传输层,用于主机之间数据的发送和接收,像web中使用的http协议便是建立在socket之上的.这一节主要讨论网络套接字. 套接 ...

  2. 解决 C# .NET WebClient WebRequest请求缓慢的问题

    [编程环境]Visual Studio 2010, NET4.0 [开发语言]C#, 理论上VB.NET等依赖.NET Framework框架的语言均受此影响 [问题描述] 使用HttpWebRequ ...

  3. Bugku-CTF加密篇之ok(Ook!)

  4. PTA----7-3树的遍历

    7-3 树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数 ...

  5. HDU1875 畅通工程再续

    相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...

  6. ANSYS 瞬态热分析---零件在水中冷却

    目录 1. 案例 2. APDL分析 1. 案例 一个温度为300℃的铜环和一个温度为200℃的铁环,放置到22℃的水中进行淬火.水桶为铁质的圆形.分析中忽略水的流动. 材料参数 热性能 铜 铁 水 ...

  7. 【PAT甲级】1048 Find Coins (25 分)(二分)

    题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出&qu ...

  8. linux修改文件的权限和修改文件所有者和所属组

    文件设定法:chmod    [who]   [+][-][=]   [mode] who 文件所有者:u 文件所属组:g 其他:o 所有人:a +  添加权限 -  减少权限 =  覆盖原来权限 权 ...

  9. centos610无桌面安装openoffice

     Centos610系列配置卸载yum remove libreoffice*yum remove openoffice* 安装yum install openoffice.org-writer yu ...

  10. CSRF预防手段

    1 referer 验证 2 token 验证