Android popupwindow 弹出的位置问题
在Android开发中,需要用到PopupWindow这个类。在初始化完成,显示之前,都需要获得这个对象的width,height去计算popupWindow弹出的位置。
这个时候会发现取得的width和height都是-2;使用popupWindow.getContentView().getMeasuredWidth()和popupWindow.getContentView().getMeasuredHeight()取得的值都是0。如下面的代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jcdh.jcli.mypopupwindow.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ShowPopupWindow"
android:onClick="showPopupWindow"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_info_bubble"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:李VV\n性别:男\n出生日期:1990/12/12 12:10:05\n所在地:北京"
android:textSize="15sp"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActitivty:
public class MainActivity extends Activity { private PopupWindow popupWindow; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPopupWindow();
} /**
* 初始化PopupWindow
*/
private void initPopupWindow() {
View <span style="background-color:#40332b;">view</span>= LayoutInflater.from(this).inflate(R.layout.popupwindow, null); popupWindow = new PopupWindow(view,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //设置背景,否则setOutsideTouchable无效
popupWindow.setOutsideTouchable(true); //设置点击PopupWindow以外的地方关闭PopupWindow
popupWindow.setFocusable(true); //获取焦点
} public void showPopupWindow(View v)
{
//点击在按钮的中上方弹出popupWindow
int btnWidth = v.getMeasuredWidth();
int btnHeight = v.getMeasuredHeight(); int popWidth = popupWindow.getContentView().getMeasuredWidth();
int popHeight = popupWindow.getContentView().getMeasuredHeight(); int xoff = (int)((float)(btnWidth - popWidth)/2);//PopupWindow的x偏移值
int yoff = popHeight+btnHeight ; //因为相对于按钮的上方,所以该值为负值 popupWindow.showAsDropDown(v,xoff,-yoff);
}
}
这时出现的效果并不是想要的效果,是在按钮是右上方出现
出现这个的原因就是因为PopupWindow的尺寸拿不到,因为内容的View的width和height都是wrap_content,所以在PopupWindow里面的contentView还没被绘制出来的时候,这两个值都还是0。
如果直接调用PopupWindow的getWidth()和getHeight(),会发现拿到的都是ViewGroup.LayoutParams.WRAP_CONTENT的值 -2;
解决的方法就是在初始化popupwindow view的时候,强制绘制view,并且马上初始化view的尺寸。这里只需要一句代码即可,加在
<span style="font-size:14px;">initPopupWindow 第二行:</span>
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
最终效果:
另外一个点需要注意:popwin_layout.xml的根Layout必须为LinearLayout;如果为RelativeLayout的话,会在第38行代码出现空指针错误,导致程序崩溃。希望看到此文章的人在开发中需要注意。
http://download.csdn.net/detail/q610098308/9333471
Android popupwindow 弹出的位置问题的更多相关文章
- Android屏幕底部弹出DialogFragment(3)
Android屏幕底部弹出DialogFragment(3) 附录文章1,2的DialogFragment是常规的DialogFragment,但是现在的一些Android开发中,往往需要从底部 ...
- 练习PopupWindow弹出框之实现界面加载的时候显示弹出框到指定的view下面--两种延迟方法
今天在练习PopupWindow弹出框的时候,打算在界面加载的时候将弹出框展现出来并显示在指定的view下面. 初步方法是直接在OnResume方法里面直接执行showPopupWindows方法. ...
- Winfrom 弹出窗体位置设定
Winfrom 窗体弹出位置设定,其实就是两种模式,第一种模式是通过Winform提供的属性来设定:第二种模式是自定义,可以相对于软件本身,也可以是相对于屏幕. 一.第一种模式 使用Winform提供 ...
- Android 对话框弹出位置和透明度的设置
在Android中 我们经常会用AlertDialog来显示对话框.通过这个对话框是显示在屏幕中心的.但在某些程序中,要求对话框可以显示在不同的位置.例如,屏幕的上 方或下方.要实现这种效果.就需要获 ...
- Android 对话框弹出位置和透明度
在Android中我们经常会用AlertDialog来显示对话框.通过这个对话框是显示在屏幕中心的.但在某些程序中,要求对话框可以显示在不同的位置.例如,屏幕的上方或下方.要实现这种效果.就需要获得对 ...
- PopupWindow --- 弹出底部窗体
第一步 : 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- PopupWindow弹出框
使用PopupWindow实现一个悬浮框,悬浮在Activity之上,显示位置可以指定 首先创建pop_window.xml: <?xml version="1.0" enc ...
- 自定义PopupWindow弹出框(带有动画)
使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { pub ...
- android一个弹出菜单的动画(二)
假设做一个弹出的控件,我们能够进行加入view: 写class SatelliteMenu extends FrameLayout private void init(Context context, ...
随机推荐
- CentOS下用pyenv 和 virtualenv 搭建单机多版本python 虚拟开发环境
安装 系统环境:CentOS 6.5 安装依赖 yum -y install gcc gcc-c++ make git patch openssl-devel zlib-devel readline- ...
- SVN Server配置详解 及备份
SVN简介和工作原理 subversion(简称svn)是近几年崛起的版本管理软件,是cvs的接班人,目前绝大多数开源软件都使用svn作为代码版本管理软件.Subversion支持linux和wind ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(三):流程模型列表展示
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- 黄聪:wp-setting.php文件详解
描述:用于设置公共变量.加载 WP 的程序和类库(存储 WP 函数.类和核心内容所在文件夹的位置).用户无需修改此文件,但是可以通过修改 wp-config.php 文件来进行配置. 定义 WPINC ...
- SIGPIPE
send或者write socket遭遇SIGPIPE信号 当服务器close一个连接时,若client端接着发数据.根据TCP协议的规定,会收到一个RST响应,client再往这个服务器发送数据时, ...
- MongoDB的基本使用
use library 使用use函数切换已有的数据库或创建新的数据库 show dbs 查看MongoDB中目前所有可用的数据库 show collections 查看当前数据库中的所有集合 在集合 ...
- IGS_学习笔记06_IREP发布客户化集成接口为Web Service(案例)
2015-01-03 Created By BaoXinjian
- NeHe OpenGL教程 第四十二课:多重视口
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- bug_ _ _android.app.Fragment$InstantiationException 解决办法
在实际的开发中,我遇到过两次android.app.Fragment$InstantiationException报错. 其中一次报错,根据报错提示 “make sure class name exi ...
- Altium Designer /DXP无网络铺铜:
有的设计者在PCB加工的时候会删除网络以便为了保护.但如果后续在无网络PCB上进行修改时就不叫麻烦,没有网络连铺铜都无法进行.一般手动添加网络只对要铺铜的地网络进行,其它的要修改者自己确保版图的正确性 ...