说明
全屏,在Android开发中,是很常见的操作。很多时候都需要全屏后,再进行自定义布局。下面就总结一下相关的几个方法。
具体方法示例如下
public class ScreenUtils {
/**
* activity全屏切换
**/
public static void changeFullScreen(Activity activity, boolean isFullscreen) {
if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
return;
}
View decorView = activity.getWindow().getDecorView();
int flag = decorView.getSystemUiVisibility();
if (isFullscreen) {
// 状态栏隐藏
flag |= View.SYSTEM_UI_FLAG_FULLSCREEN;
// 导航栏隐藏
flag |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
// 布局延伸到导航栏
flag |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
// 布局延伸到状态栏
flag |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
// 全屏时,增加沉浸式体验
flag |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
// 部分国产机型适用.不加会导致退出全屏时布局被状态栏遮挡
// activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// android P 以下的刘海屏,各厂商都有自己的适配方式,具体在manifest.xml中可以看到
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams pa = activity.getWindow().getAttributes();
pa.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
activity.getWindow().setAttributes(pa);
}
} else {
flag &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
flag &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
flag &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
flag &= ~View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
// activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams pa = activity.getWindow().getAttributes();
pa.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
activity.getWindow().setAttributes(pa);
}
}
decorView.setSystemUiVisibility(flag);
}
/**
* dialog全屏切换
**/
public void changeFullScreen(Dialog dialog, boolean isFullscreen) {
if (dialog == null) {
return;
}
Window window = dialog.getWindow();
if (window == null) {
return;
}
View decorView = window.getDecorView();
int flag = decorView.getSystemUiVisibility();
if (isFullscreen) {
// 状态栏隐藏
flag |= View.SYSTEM_UI_FLAG_FULLSCREEN;
// 导航栏隐藏
flag |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
// 布局延伸到导航栏
flag |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
// 布局延伸到状态栏
flag |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
// 全屏时,增加沉浸式体验
flag |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
// 部分国产机型适用.不加会导致退出全屏时布局被状态栏遮挡,注意加这个会导致OnGlobalLayoutListener返回的数据为10000.
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// android P 以下的刘海屏,各厂商都有自己的适配方式,具体在manifest.xml中可以看到
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams pa = window.getAttributes();
pa.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(pa);
}
} else {
flag &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
flag &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
flag &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
flag &= ~View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams pa = window.getAttributes();
pa.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
window.setAttributes(pa);
}
}
decorView.setSystemUiVisibility(flag);
}
}
总结
titlebar还需在主题,或者代码中设置为no titlebar;另外这记录的全屏方法在Android 11后被标记为过时,后面再写文章总结。还有Android版本不同,有些不一样,同样得再总结。
网友评论