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;
}
}
Android 自定义PopWindow完整代码的更多相关文章
- Android 自定义View—清爽小巧灵活的多节点进度条
前言 最近项目有一个节点进度条的小需求,完成后,想分享出来希望可以帮到有需要的同学. 真机效果图 自定义View完整代码 开箱即用~,注释已经炒鸡详细了 /** * @description: 节点进 ...
- 【转】Android自定义Adapter的ListView的思路及代码
原文网址:http://www.jb51.net/article/37236.htm Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下 在开发中,我们经常使 ...
- Android 一个相对完整的自动升级功能实现代码
由于项目的需要最近做了一个关于Android自动升级的功能,下面将贴出Android手机客户端的完整代码.这段代码参考别的代码居多,由于不满足需求,所以自己仅仅改了一些需要变动的内容,其他功能都是按照 ...
- Android 监听双卡信号强度(附完整代码)
Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- [原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android 自定义title 之Action Bar
Android 自定义title 之Action Bar 2014-06-29 飞鹰飞龙... 摘自 博客园 阅 10519 转 25 转藏到我的图书馆 微信分享: Action Ba ...
- Android 自定义ScrollView ListView 体验各种纵向滑动的需求
分类: [android 进阶之路]2014-08-31 12:59 6190人阅读 评论(10) 收藏 举报 Android自定义ScrollView纵向拖动 转载请标明出处:http: ...
- android自定义UI模板图文详解
不知道大家在实际开发中有没有自定义过UI模板?今天花时间研究了一下android中自定义UI模板,与大家分享一下. 每个设计良好的App都是自定义标题栏,在自定义标题栏的过程中大部分人可能都是自定义一 ...
随机推荐
- Spring Boot中Restful Api的异常统一处理
我们在用Spring Boot去向前端提供Restful Api接口时,经常会遇到接口处理异常的情况,产生异常的可能原因是参数错误,空指针异常,SQL执行错误等等. 当发生这些异常时,Spring B ...
- ASP.NET FileUpload 上传文件类型验证
验证的核心方法: public static bool IsAllowedExtension(FileUpload hifile) { //原方法是这样的,会提示找不到文件 //System.IO.F ...
- Spring Security教程之Jsp标签(四)
目录 1.1 authorize 1.2 authentication 1.3 accesscontrollist Spring Security也有对Jsp标签的支持的标签库 ...
- C# 抗变与协变的理解
我们知道 方法的参数是协变的: void display(shape o) 如果类Rectangle 继承于shape类,那我们可以给该方法传入Rectangle类的实例. 而方法的返回类型是抗变的, ...
- 使用vASA842配置ASDM645
准备:使用VMware打开vASA842.ova文件,将第一个网络适配器桥接到一个vmnet接口,我这里是vmnet3,网段是10.0.0.0/24,网关是10.0.0.1/24 1.打开vASA84 ...
- mark mark mark
编译并使用静态lib---->>>>转自:Walkthrough: Creating and Using a Static Library (C++) 跟踪内存分配:http: ...
- A10131013 Battle Over Cities (25分)
一.技术总结 这一题是考查图的知识,题目的意思要理解清楚,就是考查统计图中连通块的数量,也就是没有一个结点后. 怎么删除该结点,并且统计连通块的数量成为问题解决的关键,这里可以当访问到结点时,直接返回 ...
- 中国电信与小米成立5G联合创新实验室
导读 中国电信与小米成立5G联合创新实验室 近日,在中国电信战略与创新研究院,小米与中国电信共同发起的5G联合创新实验室正式揭牌成立.双方将充分发挥技术.网络.产品和生态的优势,围绕“5G+AIoT” ...
- 阿里云oss操作
参考网址 https://blog.csdn.net/qq_22764659/article/details/87969743
- eclipse下用maven插件+Mabatis-generator生成mybatis的文件
1. 配置Maven pom.xml 文件 在pom.xml增加以下插件: <build> </plugins> ... <plugin> <groupId& ...