How to limit Dialog's max height?
2024-08-25 01:45:00
1. We can make it to play trick in code.
At Dialog's show function, after app has set contentView, we can add a GlobalLayoutListener on decorView's ViewTreeObserver. At the listener, we can check the decorView's height, if over the max height, just truncate it.
private void resolveAlertDialogHeight() {
WindowManager wm = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay(); Point size = new Point();
display.getSize(size);
int height = size.y; final int maxHeight = (int) (height * 0.68); final View decorView = mWindow.getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int actualHeight = decorView.getMeasuredHeight(); WindowManager.LayoutParams l = mWindow.getAttributes();
if (actualHeight > maxHeight) {
l.height = maxHeight; mWindow.setAttributes(l);
} decorView.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
});
}
2. Modify windowBackground reference png.
The dot9 png referenced by android:windowBackground attr is the Dialog's background. We can modify this dot9 png, adding extra padding on top and bottom to limit the margin of Alert.
最新文章
- C/C++实践笔记 004
- &;12 二叉搜索树
- Object转bigdecimal
- 为什么super()和this()调用语句不能同时在一个构造函数中出现的解释
- ios开发--KVO浅析
- Java [Leetcode 292]Nim Game
- Python的传值和传址与copy和deepcopy
- AngularJs项目
- hyper-v新内容
- oracle去除字符串中间的空格
- iOS开发--in house发布和安装(ipa重新签名)
- android面试题2
- 读 《 Web 研发模式的演变 》与《Javascript:世纪机器语言》
- python的sorted
- 【4】数独(Sudoku Killer)(深度优先遍历)
- OSI网络七层协议+火了火了火
- P3379 【模板】最近公共祖先(LCA)(树链剖分)版
- 026_关于shell中的特殊变量$0 $n $* $@ $! $?
- (GoRails)使用vue和Vuex管理嵌套的JavaScript评论, 使用组件vue-map-field
- 无用之flask
热门文章
- is 与 == 的区别;小数据池; 编码与解码
- Redis + Redis-sentinel + keepalived部署过程
- jquery源码解析:jQuery原型方法init的详解
- linux使用rsync、inotify-tools实现多台服务器文件实时同步
- 使用cookie下次自动登录
- c++11 perfect forwarding
- Linux-Web应用服务性能测试初探
- TD-LTE技术原理与系统设计
- 持续集成、持续交付、持续部署、Jkens、git
- FileZilla上傳報錯:421 There are too many connections from your internet address