Android 弹出对话框Dialog充满屏幕宽度
final View view = LayoutInflater.from(context).inflate(layoutId, null);
final Dialog dialog = new Dialog(context, R.style.style_dialog);
dialog.setContentView(view);
dialog.show();
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.dialog_animation);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
style_dialog
<style name="style_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@color/white</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:scrollHorizontally">true</item> </style>
dialog_animation:
<style name="dialog_animation"> <item name="android:windowEnterAnimation">@anim/enter</item> <item name="android:windowExitAnimation">@anim/exit</item> </style>
enter:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300"/> </set>
exit:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="600"/> </set>
重要!!!
View view = LayoutInflater.from(this).inflate(R.layout.mine_phone_unbind, null); AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.DialogTheme); builder.setView(view); final AlertDialog dialog = builder.create(); Window window = dialog.getWindow(); window.setGravity(Gravity.BOTTOM); window.setWindowAnimations(R.style.dialog_animation); window.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setAttributes(lp); dialog.show();
<style name="DialogTheme" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item>移除边框 <item name="android:windowNoTitle">true</item>去除顶部标题栏 <item name="android:windowIsTranslucent">true</item>窗体透明 <item name="android:background">@android:color/white</item>背景透明 <item name="android:windowBackground">@android:color/transparent</item>窗体背景透明 <item name="android:windowIsFloating">false</item>窗体是否浮动 <item name="android:backgroundDimEnabled">true</item>背景是否昏暗 <item name="android:backgroundDimAmount">0.6</item>昏暗数量 </style>
欢迎关注我的微信公众号:安卓圈
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!