public class FullScreenDialog extends Dialog {
public FullScreenDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 按空白处不能取消
setCanceledOnTouchOutside(false);
}
@Override
public void show() {
final Window window = getWindow();
if (window != null) {
// 获取焦点,防止导航栏闪烁
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
super.show();
hideNavigationBar(window);
if (window == null) {
return;
}
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 设置全屏属性
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// 设置window背景透明
window.setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = window.getAttributes();
lp.alpha = 1.0f;
lp.dimAmount = 0.0f; //dimAmount在0.0f和1.0f之间,0.0f完全不暗,1.0f全暗
lp.format = PixelFormat.TRANSPARENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// 延伸显示区域到刘海
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
window.setAttributes(lp);
window.getDecorView().setPadding(0, 0, 0, 0);
}
/**
* 隐藏导航栏
*
* @param window window
*/
public void hideNavigationBar(final Window window) {
if (window == null) {
return;
}
final int uiOptions = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
window.getDecorView().setSystemUiVisibility(uiOptions);
}
}
全局居中dialog
sunbofiy232024-01-09 21:29
相关推荐
Pniubi6 小时前
HashMap的扩容和树化边界Mikko77 小时前
用 Java 复刻了高中时在塞班上玩的「熔岩矿车」:纯 Swing、零依赖、没有一张图片,一个 jar 双击就能玩知了学历孙老师7 小时前
小自考的校考和统考,出题方和难度差在哪Java爱好狂.7 小时前
Java初学者如何一周速通SpringBoot!秋饼8 小时前
LLM API密钥动态轮换实战:Spring Vault + Spring AI + 多Provider路由福兮说9 小时前
JSON 格式正确,数据就可靠吗?我用一份自编配置检查类型与空值紧跟先前的步伐11 小时前
Go AI Agent 实战 03|Token、上下文窗口与模型调用成本const_qiu11 小时前
第四章 00:开篇 —— 从「会聊天」到「记得住、会干活、能协作」Wang's Blog11 小时前
Java 项目实战: 交友项目 - 个人资料管理的更新接口实现与坑Promising_GEO11 小时前
GEE实战02期:从卫星波段开始:利用 Sentinel-2 自己计算 NDVI