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. 效果图
随机推荐
- 【[AH2017/HNOI2017]礼物】
题目 又是我不会做的题了 看看柿子吧 \[\sum(a_i+c-b_i)^2\] 最小化这个柿子 之所以不写下标是因为我们这个\(\{a\},\{b\}\)可以循环同构 那就开始化吧 \[\sum(a ...
- 创建git项目的feature分支以及下载特定分支的仓库代码
在掌握了基础git的使用后可以按照这个步骤来: 下载项目:git clone <仓库地址>如果要下载非master的某分支代码:git clone -b <分支名> <仓 ...
- 【luogu P1231 教辅的组成】 题解
题目链接:https://www.luogu.org/problemnew/show/P1231 对于每本书只能用一次,所以拆点再建边 #include <queue> #include ...
- 【luogu P1816 忠诚】 题解
题目链接:https://www.luogu.org/problemnew/show/P1816 用st表来解决rmq问题. 表示同时培训学的st表,然后我就忘得差不多了,在这里推荐一篇blog 大佬 ...
- Java并发程序基础
Thread.stop() 直接终止线程,并且会立即释放这个线程所持有的锁. Thread.interrupt() 并不会是线程立即退出,而是给线程发送一个通知,告知目标线程,有人希望你退出啦,至于目 ...
- Notepad++ 插件之 TextFX (安装及作用)
<安装:打开 notepad++ 插件 -> Plugin Manager -> Show Plugin Manager -> available ->选中 TextF ...
- .NET MVC伪静态
说明:MVC中的伪静态跟我们的WebForm里面的伪静态不一样(详情:配置伪静态(URL重写)) 由于我们的MVC中存在路由,所以我们不必去处理每个访问的页面进行重写,我们完全可以再配置一个路由让他显 ...
- Mac改变系统截图存储路径
时间:2018年9月13日 人物:同事和我 事情:看苹果秋季新品发布会,并且想要截图下来当时的新品介绍. 问题:系统的截图默认是存储在桌面,整理的时候,不是很方便. Mac截图快捷键: 截取选中区域: ...
- ## `nrm`的安装使用
作用:提供了一些最常用的NPM包镜像地址,能够让我们快速的切换安装包时候的服务器地址:什么是镜像:原来包刚一开始是只存在于国外的NPM服务器,但是由于网络原因,经常访问不到,这时候,我们可以在国内,创 ...
- datagrid中设置编辑,删除列是否可以访问
foreach (RepeaterItem Item in rpt_Result.Items) { LinkButton edit = (LinkButton)Item.FindControl(&qu ...