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. 效果图
随机推荐
- Python语言程序设计基础(7)—— 文件和数据格式化
返回字符串 file = input() #返回字符串 fo = open(file,"r").read(6) print(fo) 返回列表形式 file = input() fo ...
- Tinkoff Challenge - Final Round (ABC)
A题:从两个保安中间那钞票 #include <bits/stdc++.h> using namespace std; int main() { int a,b,c; scanf(&quo ...
- kiwi installation
Mainly the installstion methods follow the url: https://github.com/emolch/kiwi/wiki/Installation the ...
- Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Ticket 【。。。】
任意门:http://codeforces.com/contest/1058/problem/C C. Vasya and Golden Ticket time limit per test 1 se ...
- PHP设计模式——桥接模式
<?php /* * 桥接模式 * 使用发送器,将一个类对象传入另一个类作为属性,耦合M+N个类 * */ abstract class Info { protected $_send = NU ...
- PHP设计模式——装饰器模式
<?php /** * 装饰器模式 * 如果已有对象的部分内容或功能发生变化,但是不需要修改原始对象的结构,应使用装饰器模式 * * 为了在不修改对象结构的前提下对现有对象的内容或功能稍加修改, ...
- webstorn中的vue文件识别es6代码
webstorn中的vue文件识别es6代码 1.webstorm中es6语法报错,解决方法: 打开 Settings => Languages & Frameworks => J ...
- Latex 编辑器安装
MiKTex + TexStudio 1. 下载安装MiKTex,从http://www.miktex.org/下载basic-miktex-2.9.5872-x64.exe,一路默认安装... 或者 ...
- Visual Studio 2015 Tools for Unity安装
https://blogs.msdn.microsoft.com/visualstudio/tag/visual-studio-tools-for-unity/ 下载对应的VS版本 里边可以找到下载地 ...
- Vue node.js商城-购物车模块
一.渲染购物车列表页面 新建src/views/Cart.vue获取cartList购物车列表数据就可以在页面中渲染出该用户的购物车列表数据 data(){ return { car ...