先看效果图

黄色的就是弹出的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. rpm包形式安装MySQL

    1.下载Mysql安装所需的rpm包 http://cdn.mysql.com/Downloads/MySQL-5.6/MySQL-server-5.6.21-1.linux_glibc2.5.x86 ...

  2. Opera放弃自家内核转投WebKit的背后(转)

    Opera在2月13日宣布用户突破3亿,并且带着这3亿用户投入WebKit阵营,自家的Presto内核将会走入历史.Opera为什么选择在现在这个时间点放弃自有内核?之前Opera的坚持自主研发一直被 ...

  3. leetcode 160

    160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of ...

  4. 学习java第7天

    关于继承还需要留意的是,子类中的所有构造方法都默认访问父类的无参构造,注意是无参,而且是必须的,如果父类没有无参子类就会报错.如果你不想给父类无参构造,那么在子类中加上super(),显式的调用有参构 ...

  5. GRIDVIEW 控件

    http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html GridView控件是.net里的一个显示数据控件,该控件制作很人性化, ...

  6. delphi XE Berlin ReadProcessMemory WriteProcessMemory

    delphi  XE,Berlin [dcc32 Error] Unit9.pas(93): E2033 Types of actual and formal var parameters must ...

  7. Visual Studio 2015完全离线安装

    虽然微软提供了Visual Studio的ISO镜像下载,但这个ISO文件并不完整,安装的过程中依然需要联网下载一些安装包,在中国特色的网络环境下导致安装过程还是非常慢的.另外,在一些网络隔离的环境中 ...

  8. fis3安装

    主要安装过程参考官网:http://fis.baidu.com/fis3/docs/beginning/install.html 这里记录安装fis3时遇到的一些问题: 1.npm install - ...

  9. MySQL_关于用嵌套表计算的可以不用 20161205

    计算求和类的指标,其实用不到嵌套表,比如计算各城市产品分类的订单额. 如果要计算不重复的指标 比如一个用户一天下了多个订单 用这样的表计算一天有多少用户下单 这个用户肯定是去重的 下多个订单也应该视为 ...

  10. A potentially dangerous Request.Form value was detected from the client

    提交表单中包含特殊字符如<script>可能被认为是跨站攻击代码:解决方法很多,如stackoverflow上的web.config中加设置的方法不中肯[如原贴中Jamie M所说],主要 ...