一直以来都觉得 Android 中的 PopupWindow 不好用。主要有以下两点:
1、宽度不好控制
2、位置不好控制

今天单说第1点。

由于应用有好几种国家的语言,加上各设备宣染效果不完全一样,对popupWindow宽度肯定不能写固定值,一般用系统提供的WRAP_CONTENT,但显示的效果往往不是预期的。

怎样才能正确显示呢,我们可以根据内容来计算宽度(找所有内容中最长的一个),代码如下:

private int measureContentWidth(ListAdapter listAdapter) {
ViewGroup mMeasureParent = null;
int maxWidth = 0;
View itemView = null;
int itemType = 0; final ListAdapter adapter = listAdapter;
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int count = adapter.getCount();
for (int i = 0; i < count; i++) {
final int positionType = adapter.getItemViewType(i);
if (positionType != itemType) {
itemType = positionType;
itemView = null;
} if (mMeasureParent == null) {
mMeasureParent = new FrameLayout(mContext);
} itemView = adapter.getView(i, itemView, mMeasureParent);
itemView.measure(widthMeasureSpec, heightMeasureSpec); final int itemWidth = itemView.getMeasuredWidth(); if (itemWidth > maxWidth) {
maxWidth = itemWidth;
}
} return maxWidth;
} //调用
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1, STRINGS);
popupWindow.setFocusable(true);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(measureContentWidth(adapter));

效果:

这样就可以了。

Android PopupWindow 疑难杂症之宽度WRAP_CONTENT的更多相关文章

  1. Android PopupWindow的使用和分析

    Android PopupWindow的使用和分析 PopupWindow使用 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activi ...

  2. Android popupwindow 弹出的位置问题

    在Android开发中,需要用到PopupWindow这个类.在初始化完成,显示之前,都需要获得这个对象的width,height去计算popupWindow弹出的位置. 这个时候会发现取得的widt ...

  3. Android PopupWindow的使用技巧(转)

    Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...

  4. 简单 android popupwindow 实现

    1. popupWindow 设置大小: popupWindow = new  PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGr ...

  5. Android PopupWindow Dialog 关于 is your activity running 崩溃详解

    Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...

  6. android:PopupWindow的使用场景和注意事项

    1.PopupWindow的特点 借用Google官方的说法: "A popup window that can be used to display an arbitrary view. ...

  7. Android PopupWindow增加半透明蒙层

    先看效果图: BasePopupWindowWithMask.class package com.example.popupwindowwithmask; import android.content ...

  8. 不得不吐槽的Android PopupWindow的几个痛点(实现带箭头的上下文菜单遇到的坑)

    说到PopupWindow,我个人感觉是又爱又恨,没有深入使用之前总觉得这个东西应该很简单,很好用,但是真正使用PopupWindow实现一些效果的时候总会遇到一些问题,但是即便是人家的api有问题, ...

  9. Android -- PopupWindow(其中嵌套ListView 可以被点击)

    1. 效果图

随机推荐

  1. 清除IE地址栏中的历史网址

    实现效果: 知识运用: RegistryKey类的GetValueNames和DeleteValue方法 实现代码: private void button1_Click(object sender, ...

  2. 2018.12.22 Spring学习02

    Spring学习02 1.使用注解配置Spring 1.1 为主配置文件引入新的命名空间(约束) 添加约束文件xxx-xxx-context.xml 添加到主配置文件中 选择刚才的context.xm ...

  3. SpringMVC学习记录七——sjon数据交互和拦截器

    21       json数据交互 21.1      为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservi ...

  4. 【luogu P2065 [TJOI2011]卡片】 假题解

    题目链接:https://www.luogu.org/problemnew/show/P2065 辣鸡匈牙利,没有优化贼鸡儿慢 // luogu-judger-enable-o2 #include & ...

  5. Android学习笔记_43_网络通信之文件断点上传

    1.建立服务端,用于接收上传的文件.这里使用Socket,文件可能会比较大.采用多线程编程,防止并发. package com.socket.service; import java.io.File; ...

  6. 火狐 SSL 收到了一个弱临时 Diffie-Hellman 密钥

    火狐 SSL 收到了一个弱临时 Diffie-Hellman 密钥   最近在用FireFox 调试时使用Https,连接 https网址 时发生错误. 在服务器密钥交换握手信息中 SSL 收到了一个 ...

  7. JavaScript js调用堆栈(一)

    本文主要介绍JavaScript程序内部的执行机制 首先先了解什么是执行上下文 执行上下文就是当前JavaScript代码被解析和执行是所在环境的抽象概念,JavaScript中运行任何的代码都是在执 ...

  8. Java实现非递归归并排序

    public class nonRecursiveMergeSort { public static void main(String[] args) { int[] list = {8,4,3,6, ...

  9. flex 遇到white-space:nowrap

    背景,做一个前面图片宽度固定,后面宽度自适应,使用到了flex布局,但是想让后面div里文字不换行,超出以点点表示时,这时布局就乱了,查了下,原来flex布局与white-space:nowrap有影 ...

  10. vue和react总结

    一.总结一下Vue和React的区别 相同点:   1.都支持服务器端渲染     2.都有Virtual DOM,组件化开发,通过props参数进行父子组件数据的传递,都实现webComponent ...