在指定控件位置弹出popup window
先看效果图
黄色的就是弹出的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的更多相关文章
- 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
[源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...
- 控件(弹出类): ToolTip, Popup, PopupMenu
示例1.ToolTip 的示例Controls/FlyoutControl/ToolTipDemo.xaml <Page x:Class="Windows10.Controls.Fly ...
- 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout
[源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...
- 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog
[源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...
- ocx控件避免弹出警告的类--2
本文与 OCX控件避免弹出安全警告的类 http://www.cnblogs.com/lidabo/archive/2013/03/26/2981852.html 有些类似,只不过增加了几行代码(红色 ...
- 小程序中点击input控件键盘弹出时placeholder文字上移
最近做的一个小程序项目中,出现了点击input控件键盘弹出时placeholder文字上移,刚开始以为是软键盘弹出布局上移问题是传说中典型的fixed 软键盘顶起问题,因此采纳了网上搜到的" ...
- 控件(弹出类): FlyoutBase, Flyout, MenuFlyout
1.FlyoutBase(基类) 的示例Controls/FlyoutControl/FlyoutBaseDemo.xaml <Page x:Class="Windows10.Cont ...
- 问题-PopupMenu是哪个控件调用弹出的?
相关资料: http://bbs.csdn.net/topics/310195683 问题现象:今天有朋友问我个简单的问题,在多个Edit上弹出菜单,怎么判断是哪个Edit调用的.我想了想这个我还真不 ...
- C# 禁止 Webbrowser 控件的弹出脚本错误对话框
当IE浏览器遇到脚本错误时浏览器,左下 角会出现一个黄色图标,点击可以查看脚本错误的详细信息,并不会有弹出的错误信息框.当我们使用 WebBrowser控件时有错误信息框弹出,这样程序显的很不友好,而 ...
随机推荐
- The default for KeyValuePair
if (getResult.Equals(new KeyValuePair<T,U>())) or this: if (getResult.Equals(default(KeyValueP ...
- JAVA设计模式之解释器模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述解释器(Interpreter)模式的: 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个 ...
- testng 控制case运行顺序
Testing.xml 文档结构: <test name="xxxx" preserve-order="false"> <!-- 参数定义的方 ...
- 高亮代码显示之HTML困惑
近期做样式库,需要将HTML代码高亮,开始寻找相关的插件. 看到highlight.js,看到它主题样式如此之多,支持语言也如此之多,以为找到了神器.不想这只是痛苦的开始,为了让它支持HTML,我尝试 ...
- 使用CodeSmith快速生成映射文件和映射类
一 CodeSmith简介 本文以表自动生成NHibernate的映射文件和映射类的实例来说明一下本软件的使用方法. CodeSmith是一种基于模板的代码生成工具,其使用类似于ASP.NET的语法来 ...
- Swift 协议
/// 一般情况下,定义的协议都必须实现 protocol SomeProtocal { func doSomething() } /// 定义一个类,并且遵守协议 class Teacher:Som ...
- ajax跨域请求带cookie
调用网站:a.xxx.com jQuery(document).ready(function () { $.ajax({ type: "get", async: true, url ...
- 远程操控软件 TeamViewer for MAC 官方中文版11.0.55321
百度云:https://pan.baidu.com/s/1o77ol2y 提取密码:3tsx windows: http://download.pchome.net/internet/server/r ...
- {matlab}取二值图像centroid几种方法性能比较
试验很简单,取二值图像的质心,三种方法做比较 1.完全采用矩阵性能不做任何循环操作,对find后的值进行除法与取余操作,从而得到centroid 2.完全采用循环操作,最简单明了 3.结合1,2,对每 ...
- VC++ list函数详解
在使用之前,需要完成两件事: (1) #include <list> (2) using namespace std; 声名变量: list<int> intlist ...