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都是自定义标题栏,在自定义标题栏的过程中大部分人可能都是自定义一 ...
随机推荐
- 1、MyBatis框架底层初涉
1.拜年 哈哈,现在是过年了,祝大家新年好. 本来大过年的是不打算碰电脑的,(抢票除外,三疯同学现在还没抢到票,然后突然又延长假期了).现在疫情严重,被堵家里不能出去了.不能为国家做贡献,但是起码不能 ...
- Spring Boot 学习前你应该知道的 Maven 知识
Maven 是什么? 回答这个问题,我们先来了解下没有Maven,我们是怎么使用开发者工具IDE去开发Java程序的.我之前开发Java程序不多,但是我还是记得,我是从网上下载或从合作方拷贝 jar ...
- 无线连接网络-FAST SSID Change
这篇随笔主要记录的是Apple设备连接思科无线可能出现的问题,尤其是在思科WLC3504下部署的无线网络,这种故障体现的尤为明显. For Single SSID To support Apple i ...
- Java面向对象编程 -2.2
构造方法与匿名对象 现在的程序在使用类的时候一般都按照了如下的步骤进行: 声明并实例化对象,这个时候实例化对象中的属性并没有任何的数据存在,都是其对于数据类型的默认值 需要通过一系列的setter方法 ...
- spring boot 整合mapreduce运行的ClassNotFoundException
问题 一个wordcount运行总是报错 java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.hadoop. ...
- __str__()方法和__repr__()方法
有时候我们想让屏幕打印的结果不是对象的内存地址,而是它的值或者其他可以自定义的东西,以便更直观地显示对象内容,可以通过在该对象的类中创建或修改__str__()或__repr__()方法来实现(显示对 ...
- onclick="this.src=this.src+'?'"是什么意思?
onclick="this.src=this.src+'?'" 这是表示当前图片链接 在当前链接值的基础上添加了一个问号 譬如当前src="check.aspx" ...
- iOS 开发之应用内弹出 App Store 应用界面
在APP内给其他APP做推广,经常用到在应用内弹出应用的APP #import <StoreKit/SKStoreProductViewController.h> 设置代理:<SKS ...
- selected中第一项 请选择,隐藏
如何做到selected 类似input的提示语 placeholder效果. <select class="wyj_dbfs"> <option style= ...
- 同一条sql insert 有时快有时慢 引发的血案
同一条sql语句,为什么有时插入块,有时插入慢原因剖析 背景:同一条sql ,有时插入时间几毫秒,有时插入时间几十毫秒,为什么? Sql角度:简单insert 表角度: 一个主键 系统参数角度: 开启 ...