先看效果图

黄色的就是弹出的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. [SharpZipLib 未能加载文件或程序集] 解决方法

    未能加载文件或程序集"ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6a ...

  2. Sublime Text对Python代码加注释的快捷键

    一直在Coursera上补基础课,发现很多课程都用Python作为教学语言,学了一下感觉果然好,简直是用英语在写代码.(我建Python目录的时候发现去年学过一点点Python,居然一点都不记得了= ...

  3. [EventBus源码解析] EventBus.post 方法详述

    前情概要 上一篇blog我们了解了EventBus中register/unregister的过程,对EventBus如何实现观察者模式有了基本的认识.今天我们来看一下它是如何分发一个特定事件的,即po ...

  4. tinyxml学习一

    在TinyXML中,根据XML的各种元素来定义了一些类:         TiXmlBase:整个TinyXML模型的基类.         TiXmlAttribute:对应于XML中的元素的属性. ...

  5. lua中的协程

    lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...

  6. AngularJS图片上传功能的实现

    一.前言 前一段时间做项目时,遇到一个问题就是AngularJS实现图片预览和上传的功能,当时查阅文档(都是英文文档)折腾了很久才弄出来,现将整个流程整理出来,有需要的朋友可以参考一下,如果您有更好的 ...

  7. 使用DotNetOpenAuth搭建OAuth2.0授权框架——Demo代码简单说明

    前段时间随意抽离了一部分代码作为OAuth2的示例代码,若干处会造成困扰,现说明如下: public class OAuthController : Controller { private stat ...

  8. UITaleView的基础使用及数据展示操作

    UITableView表视图,是实用的数据展示的基础控件,是继承于UIScrollView,所以也可以滚动.但不同于UIScrollView,UITableView只可以上下滚动,而不能左右滚动. 因 ...

  9. php关闭错误提示

    今天调试phalcon的一个接口时候碰到如下提示: Deprecated: mongogo::mongogo(): The Mongo class is deprecated, please use ...

  10. PHP echo 即时输出

    header(“Content-type:text/html;charset=utf-8″); #设置执行时间不限时 set_time_limit(0); #清除并关闭缓冲,输出到浏览器之前使用这个函 ...