Android PopupWindow 疑难杂症之宽度WRAP_CONTENT
一直以来都觉得 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的更多相关文章
- Android PopupWindow的使用和分析
Android PopupWindow的使用和分析 PopupWindow使用 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activi ...
- Android popupwindow 弹出的位置问题
在Android开发中,需要用到PopupWindow这个类.在初始化完成,显示之前,都需要获得这个对象的width,height去计算popupWindow弹出的位置. 这个时候会发现取得的widt ...
- Android PopupWindow的使用技巧(转)
Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...
- 简单 android popupwindow 实现
1. popupWindow 设置大小: popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGr ...
- Android PopupWindow Dialog 关于 is your activity running 崩溃详解
Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...
- android:PopupWindow的使用场景和注意事项
1.PopupWindow的特点 借用Google官方的说法: "A popup window that can be used to display an arbitrary view. ...
- Android PopupWindow增加半透明蒙层
先看效果图: BasePopupWindowWithMask.class package com.example.popupwindowwithmask; import android.content ...
- 不得不吐槽的Android PopupWindow的几个痛点(实现带箭头的上下文菜单遇到的坑)
说到PopupWindow,我个人感觉是又爱又恨,没有深入使用之前总觉得这个东西应该很简单,很好用,但是真正使用PopupWindow实现一些效果的时候总会遇到一些问题,但是即便是人家的api有问题, ...
- Android -- PopupWindow(其中嵌套ListView 可以被点击)
1. 效果图
随机推荐
- 【洛谷2290】[HNOI2004] 树的计数(Python+利用prufer序列结论求解)
点此看题面 大致题意: 给定每个点的度数,让你求有多少种符合条件的无根树. \(prufer\)序列 这显然是一道利用\(prufer\)序列求解的裸题. 考虑到由\(prufer\)序列得到的结论: ...
- 「SDOI2008沙拉公主的困惑」
题目 看着有点可怕 求 \[\sum_{i=1}^{n!}[(i,m!)=1]\] 考虑一下\(m=n\)的时候的答案 非常显然就是\(\varphi(m!)\) 而如果\(n>m\) 非常显然 ...
- react工程目录简介
首先我们可以看到底部一个 yarn.lock的文件,这个文件主要是项目依赖的安装包,他的一些版本号,会在这里做一些限制,这个文件不要动就好了. readme.md,这个文件其实是项目的说明文件,他里面 ...
- Runloop理解
看了一堂公开课,自己小结一下: Runloop: 内部有三个东东:(Source, Timer, Observer) 作用/本质:1.死循环 (为app 保活): 2.监听处理事件 Timer 理解: ...
- 实现新layer的时候易犯的错误
实现新layer后,如果我还是在原来的build文件夹里面make,好像是不会编译新的层的,所以跑程序会报没有你新添加的这个层.要么make clear重新make,要么就直接./build.sh,这 ...
- 【洛谷P4124】[CQOI2016]手机号码
手机号码 数位DP模板题 记忆化搜索: #include<iostream> #include<cstring> #include<cstdio> using na ...
- Controller类的方法上的RequestMapping一定要写在Controller类里吗?
转载请标明出处: https://blog.csdn.net/forezp/article/details/80069961 本文出自方志朋的博客 使用Spring Cloud做项目的同学会使用Fei ...
- Django-rest-framework(八)使用技巧(持续补充中)
当viewset中,多个action对应了不同的serializers的时候 class UserView(viewsets.GenericViewSet): queryset = User.obje ...
- java之递归学习
递归思想(2018-10-22): 递归就是方法里调用自身 在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口 递归算法代码显得很简洁,但递归算法解题的运行效率较低.所以不提倡用递归设计程序 ...
- WebSocket 和socket 的区别
去年光棍节的时候,我写过一篇 quick-cocos2d-x 中的 socket 技术选择:LuaSocket 和 WebSocket .这篇文章介绍了我为何决定在项目中使用 LuaSocket . ...