先看效果图

黄色的就是弹出的popup window

首先自定义一个view用来显示,文件名为layout_my_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_tips"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textColor="#333333"
android:text="。。。"/> </LinearLayout> java 代码实现
public class TipsView extends LinearLayout {
public TipsView(Context context) {
super(context);
init();
} public TipsView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} @TargetApi(Build.VERSION_CODES.HONEYCOMB)
public TipsView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
inflate(getContext(), R.layout.layout_my_view, this);
}
}

调用代码

private void showTips() {
if (mTipsView == null) {
mTipsView = new TipsView(this);
mPopupWindow = new PopupWindow(mTipsView, RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT, true);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
mPopupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color
.transparent));
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
} if (mPopupWindow.isShowing()) {
return;
} // 设置好参数之后再show
int local[] = new int[2];
//弹出控件的位置,坐标存在local数组
mTvBindFlag.getLocationOnScreen(local); int width = mTipsView.getWidth();
int height = mTipsView.getHeight();
if (width == 0 || height == 0) {
// 获取测量后的宽度
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mTipsView.measure(w, h);
width = mTipsView.getMeasuredWidth();
height = mTipsView.getMeasuredHeight();
} // x坐标计算方式:complete_count_txt的x坐标加上他的长度一半(相当于complete_count_txt的横向居中位置)
// ,再减少弹出气泡宽度的一半(相当于向左移动气泡一半的宽度,就居中显示在complete_count_txt了)
int x = local[0] + (mTvBindFlag.getWidth() / 2) - width / 2;
// y坐标计算方式:complete_count_txt的y坐标减去气泡的高度
int y = local[1] - height;
// 通过绝对位置显示
@param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
* @param gravity the gravity which controls the placement of the popup window
* @param x the popup's x location offset
* @param y the popup's y location offset
*/
// public void showAtLocation(View parent, int gravity, int x, int y)
mPopupWindow.showAtLocation(mTvBindFlag, Gravity.NO_GRAVITY, x, y);
}
 

在指定控件位置弹出popup window的更多相关文章

  1. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

  2. 控件(弹出类): ToolTip, Popup, PopupMenu

    示例1.ToolTip 的示例Controls/FlyoutControl/ToolTipDemo.xaml <Page x:Class="Windows10.Controls.Fly ...

  3. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  4. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  5. ocx控件避免弹出警告的类--2

    本文与 OCX控件避免弹出安全警告的类 http://www.cnblogs.com/lidabo/archive/2013/03/26/2981852.html 有些类似,只不过增加了几行代码(红色 ...

  6. 小程序中点击input控件键盘弹出时placeholder文字上移

    最近做的一个小程序项目中,出现了点击input控件键盘弹出时placeholder文字上移,刚开始以为是软键盘弹出布局上移问题是传说中典型的fixed 软键盘顶起问题,因此采纳了网上搜到的" ...

  7. 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    1.FlyoutBase(基类) 的示例Controls/FlyoutControl/FlyoutBaseDemo.xaml <Page x:Class="Windows10.Cont ...

  8. 问题-PopupMenu是哪个控件调用弹出的?

    相关资料: http://bbs.csdn.net/topics/310195683 问题现象:今天有朋友问我个简单的问题,在多个Edit上弹出菜单,怎么判断是哪个Edit调用的.我想了想这个我还真不 ...

  9. C# 禁止 Webbrowser 控件的弹出脚本错误对话框

    当IE浏览器遇到脚本错误时浏览器,左下 角会出现一个黄色图标,点击可以查看脚本错误的详细信息,并不会有弹出的错误信息框.当我们使用 WebBrowser控件时有错误信息框弹出,这样程序显的很不友好,而 ...

随机推荐

  1. 【knowledgebase】如何知道partition数

    对于调优和排错来说,查看一个RDD有多少个partition是非常有用的.常用的查看方法有如下几种: 1.通过SparkUI查看Task执行的partition数 当一个stage执行时,能通过Spa ...

  2. Centos中的Docker 配置:将loop-lvm改为derict-lvm

    重新装了个虚拟机,回顾一下最近三天的工作: Centos 查看版本 cat /etc/redhat-release yum -y upgrade 升级所有包,不改变软件设置和系统设置,系统版本升级,内 ...

  3. java泛型中的super和extend

    List<? extend Fruit> list=new ArrayList<>();  解释为:集合中元素是继承自Fruit,究竟是何种类型,编译器也无法判定. 如果要从集 ...

  4. mac配置vim-go

    基本的设置信息(参考网址:http://hessian.cn/p/1026.html): "还是配置/.vimrc文件. syn on "语法支持 set laststatus=2 ...

  5. WinServer2008r2 机器时间格式修改

    windows2008 这么高级的系统不可能改个系统的日期时间显示格式还要进注册表啊.于是有baidu,google了下终于发现了,原来还有不需要注册表的更简便方法.windows2008默认时间格式 ...

  6. android开发--布局三(微信布局)

    微信主界面 我们布局应该从局来看,如上图,我们可以分为三个大的LinearLayout,再从LinearLayout嵌套其它布局,从而做出界面 文件 主界面代码: <LinearLayout x ...

  7. px,pt,em,rem

    一直对px,pt,em,rem的认识有误区,现整理一下,供参考.之后还得整理下关于dpi相关的知识. px(pixe,像素l):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物 ...

  8. SHELL脚本攻略(学习笔记)--2.5 tr

    tr主要用于映射结果集.压缩和删除字符.我个人感觉特别有用,特别是压缩连续空格(空行)为一个空格(空行),让不规则的信息变得规则. 2.5.1 tr映射 tr [options] [SET1] [SE ...

  9. mysql的sql_mode 模式修改 my.cnf

    1. sql_mode模式 mysql数据库的中有一个环境变量sql_mode,定义了mysql应该支持的sql语法,数据校验等!我们可以通过以下方式查看当前数据库使用的sql_mode: mysql ...

  10. C++ JsonCpp 使用(含源码下载)

    C++ JsonCpp 使用(含源码下载) 前言 JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择JSON的全称为:JavaScri ...