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. 效果图
随机推荐
- struts2+extjs文件上传完整实现(攻克了上传中的各种问题)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shanhuhau/article/details/28617999 首先须要引入上传控件 <s ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】
任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...
- Android学习笔记_75_Andorid控件样式汇总
<!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...
- LeetCode16.最接近的三数之和 JavaScript
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- 固定导航栏demo
代码如下 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- 18.Shiro与Springboot整合下登陆验证UserService未注入的问题
Shiro与Springboot整合下登陆验证UserService未注入的问题 前言: 刚开始整合的情况下,UserService一执行,就会报空指针异常. 看了网上各位大神的讲解,什么不能用ser ...
- Question 20171116 StringBuffer和StringBuilder的扩容机制
StringBuffer和StringBuilder都是继承自AbstractStringBuilder,它们两个的区别在于buffer是线程安全的,builder是线程不安全的,前者安全效率低,后者 ...
- Oracle 差异性增量 和 累计增量 原理(转)
RMAN一个强大的功能是支持增量备份,增量备份中心思想就是减少备份的数据量,我们不需要在从头开始备份了,只需要备份自上次已备份之后的数据块即可. Oracle 9i 共有五种级别 0 1 2 3 ...
- HTML表格和表单
<table>格式: 注意:1. 合并单元格:COLSPAN(跨列)ROWSPAN(跨行) 2.cellspacing属性定义单元格之间的间距(以像素为单位). cellpadding属性 ...
- iOS之报错“Cannot create __weak reference in file using manual reference counting”解决办法
解决的办法:在Build Settings--------->Aplle LLVM8.0 - Language - Objectibe-C------------->Weak Refere ...