一直以来都觉得 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. Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired,

    Did not receive a reply. Possible causes include: the remote application did not send a reply, the m ...

  2. LG3690 【【模板】Link Cut Tree (动态树)】

    题目 终于去写\(LCT\)了 这个大爷讲的挺好的 板子 #include<algorithm> #include<iostream> #include<cstring& ...

  3. 【[JSOI2007]文本生成器】

    \(AC\)机上的计数\(dp\)啊 并没有想到反着求出不合法的串的个数,直接正面硬上 设\(dp[i][j][0/1]\)表示匹配出的长度为\(i\),在\(AC\)机上位置为\(j\),没有/有匹 ...

  4. 使用intellij idea创建JSP和引入Tomecat包

    首先我们需要按照好intellij idea 如果没有安装好,这里有下载按照教程:https://www.cnblogs.com/weibanggang/p/9395702.html 首先我们创建一个 ...

  5. Android学习笔记_56_应用Tween动画 (渐变、缩放、位移、旋转)

    1.实现listview每个项先向右移动,再向左移动(回到原来位置) TranslateAnimation ta = new TranslateAnimation( Animation.RELATIV ...

  6. SqlSugar之DbContext

    创建一个DbContext和DbSet进行使用,我们可以在DbSet中进行扩展我们的方法 //可以直接用SimpleClient也可以扩展一个自个的类 //推荐直接用 SimpleClient //为 ...

  7. c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)

    #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define ...

  8. 协议类接口 - I2C

    一.12c总线概述 I2C( Inter-Integrated Circuit,又称IIC)总线是一种串行总线,用 于连接微控制器及其外围设备,硬件图非常简单:一条串行数据线(SDA),一条串行时钟线 ...

  9. [UNIX]UNIX常用命令总结

    (1)查看服务器IP信息 $netstat -in (2)查看挂载磁盘信息 #sam #需要在root账号下查看

  10. oracle client安装与配置

    (一)安装Oracle client 环境:windows7 64-bit.oracle client 64-bit (1)解压client安装包 (2)双击setup.exe,选择管理员,一直nex ...