Android布局——水滴屏全屏设置

提出问题:在水滴屏Android手机上如何实现全屏展示。

探究发现
1、ViewGroup.LayoutParams(官网解释,Summary)
该类继承自java.lang.Object。该类的属性很少,因为只有三个变量和三个int型常量,如下表所示。它还有三个不同的构造函数,其中包含一个copy构造函数。

常量
FILL_PARENT、MATCH_PARENT、WRAP_PARENT
变量
height、width、layoutAnimationParameters

2、WindowManager.LayoutParams(官网解释)
该类直接继承ViewGroup.LayoutParams,并且实现了接口Parcelable。它的内容比它的父类多了很多。它的属性中有大量的常量(所以,单独将这些常量拿到一篇博客中,点击此处可以浏览这些常量)。关于布局的常量见下面所示。


  • int FLAGS_CHANGED
  • int FLAG_FULLSCREEN
    This constant was deprecated in API level R. Use WindowInsetsController#hide(int) with Type#statusBars() instead.
    最新的Android 11建议废弃这个常量。
    Window flag: hide all screen decorations (such as the status bar) while this window is displayed. This allows the window to use the entire display space for itself – the status bar will be hidden when an app window with this flag set is on the top layer. A fullscreen window will ignore a value of SOFT_INPUT_ADJUST_RESIZE for the window’s softInputMode field; the window will stay fullscreen and will not resize.
    窗口标志:显示此窗口时隐藏所有屏幕装饰(如状态栏)。这允许窗口为自己使用整个显示空间——当设置了此标志的应用程序窗口位于顶层时,状态栏将被隐藏。全屏窗口将忽略窗口的softInputMode字段的SOFT_INPUT_ADJUST_RESIZE值;窗口将保持全屏,并且不会调整大小。
  • int FLAG_LAYOUT_NO_LIMITS
    Window flag: allow window to extend outside of the screen.
    窗口标志:允许窗口扩展到屏幕之外。

  • int LAYOUT_CHANGED
  • int LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS
    The window is always allowed to extend into the DisplayCutout areas on the all edges of the screen.
    窗口总是可以扩展到屏幕所有边缘上的显示剪切区域。
  • int LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
    The window is allowed to extend into the DisplayCutout area, only if the DisplayCutout is fully contained within a system bar.
    仅当系统栏中完全包含DisplayCutout时,才允许窗口扩展到DisplayCutout区域。
  • int LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
    The window is never allowed to overlap with the DisplayCutout area.
    窗口永远不允许与显示剪切区重叠。
  • int LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
    The window is always allowed to extend into the DisplayCutout areas on the short edges of the screen.
    窗口总是可以延伸到屏幕短边上的显示剪切区域。

变量:

  • public int flags
    Various behavioral options/flags.
  • public int layoutInDisplayCutoutMode
    Controls how the window is laid out if there is a DisplayCutout.
  • public int systemUiVisibility
    This field was deprecated in API
    level R. SystemUiVisibility flags are deprecated. Use
    WindowInsetsController instead.

3、如何将参数设置到目的对象
因为调用Activity.getWindow()方法可以返回一个Window对象,进而通过Window.getAttributes()方法可以获得WindowManager.LayoutParams对象。

//某个Avtivity的onCreate方法中
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();

解决方案

public static void fullScreen(final Activity activity, final boolean enable) {
        if(activity == null){
            return;
        }
        Window window = activity.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        if (enable) {
            layoutParams.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            layoutParams.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
            layoutParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
        window.setAttributes(layoutParams);
    }

感谢微博 有个小男神 https://blog.csdn.net/qq912098812/article/details/77369872

你可能感兴趣的:(安卓Android)