当前位置:网站首页>自定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” 不起作用。
边栏推荐
- The MySQL database in Alibaba Cloud was attacked, and the data was finally recovered
- Build vulhub vulnerability shooting range on kali
- sqlmap注入教程 常用指令
- Memcached :安装
- Volatility取证工具使用日记
- Artifact SSMwar exploded Error deploying artifact.See server log for details
- The server time zone value ‘й‘ is unrecognized or represents more than one time zone
- Hyper-V新建虚拟机注意事项
- 2021面经-拥抱变化
- How to distinguish big and small endian in C language
猜你喜欢
随机推荐
纯shell实现文本替换
最新MySql安装教学,非常详细
Fragmented NFT (Fractional NFT)
常见JVM面试题及答案整理
代码执行漏洞
MySQL高级语句(一)
数据库 | SQL查询进阶语法
js中流程控制语句
[JVM Loading]---Class Loading Mechanism
Several solutions for mysql startup error The server quit without updating PID file
NFTs: The Heart of Digital Ownership
【JVM加载】---类加载机制
Linux中mysql密码修改方法(亲测可用)
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
腾讯云GPU桌面服务器驱动安装
Year-end summary - the years are quiet~
Volatility取证工具使用日记
About integrating superset into your own project