先看效果图

黄色的就是弹出的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. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  2. Ubuntu中启用关闭Network-manager网络设置问题!

    Ubuntu中启用关闭Network-manager网络设置问题! [Server版本] 在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/net ...

  3. Spark JdbcRDD 简单使用

    package org.apache.spark.sql.sources import org.apache.spark.SparkContext import java.sql.{ResultSet ...

  4. Codeforces Round #229 (Div. 2) D

    D. Inna and Sweet Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  6. TextView 中添加超链接

    在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址,一种是设置autolink,让系统自动识别超链接,下面为大家介绍下这两种方法的实现   代码如下:    第一种    pu ...

  7. 比较一下Linux下的Epoll模型和select模型的区别

    一. select 模型(apache的常用) 1. 最大并发数限制,因为一个进程所打开的 FD (文件描述符)是有限制的,由 FD_SETSIZE 设置,默认值是 1024/2048 ,因此 Sel ...

  8. nginx配置文件注释

    #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...

  9. I can connect to an FTP site but I can't list or transfer files.

    原文 FTP sessions use two network connections: The control channel is for user authentication and send ...

  10. matlab squeeze函数的用法

    matlab中squeeze函数用于删除矩阵中的单一维(Remove singleton dimensions),比如执行下面的代码,随机产生一个1x2x3的矩阵A,然后squeeze(A)将返回一个 ...