当前位置:网站首页>自定dialog 布局没有居中解决方案
自定dialog 布局没有居中解决方案
2022-07-31 05:15:00 【为今天而努力】
自定义的 dialog 忽然跑到左上角了?
- 网上提供的解决方案解决了部分手机适配
Window dialogWindow = getWindow();
dialogWindow.setGravity(Gravity.CENTER);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialogWindow.setAttributes(lp)
但是小米手机 dialog 出现偏左的现象
开启试图查看原因是内容没有占满 dialog 的全部内容,
实现方式
FrameLayout.LayoutParams vLp = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
vLp.gravity = Gravity.CENTER;
setContentView(grabDialog, vLp);
原理
使用 setContentView(View view) 的重载方法。
setContentView(View view, ViewGroup.LayoutParams lp);
其实是将该布局添加到 FrameLayout 布局中,所以可以直接设置布局的加载限制
注意
xml 设置了android:layout_gravity=“center” 不起作用。
边栏推荐
猜你喜欢
年终总结——岁月静好~
MySQL高级语句(一)
GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?
gin框架学习-Casbin进阶之策略管理API使用方法
[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version
Year-end summary - the years are quiet~
计网 Packet Tracer仿真 | 简单易懂集线器和交换机对比(理论+仿真)
CMOS管原理,及其在推挽电路中的应用
Why is the redis single-threaded also so fast?
什么是EVM兼容链?
随机推荐
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
C language tutorial (1) - preparation
GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?
MySQL高级语句(一)
js中的this指向与原型对象
理解js运算符
leetcode-1833. 雪糕的最大数量(排序+贪心)
Eternal blue bug reappears
【swagger关闭】生产环境关闭swagger方法
【windows】--- SQL Server 2008 超详细安装教程
vulhub靶场学习日记hackme1
2021美赛C题M奖思路
Build vulhub vulnerability shooting range on kali
leetcode-每日一题558. 四叉树交集(分治递归)
Why is the redis single-threaded also so fast?
代码块、Package,Import,封装(第六天)
为什么bash中的read要配合while才能读取/dev/stdin的内容
vulhub靶场学习日记hackme2
[Elastic-Job] Overview of Distributed Scheduling Tasks
元宇宙的前景及四大赛道