SystemUIOverlayWindow
管理系統提供一種方式,可在 SystemUIOverlayWindow
中顯示及管理檢視畫面。目前,這個視窗用於全螢幕使用者切換器、通知面板和螢幕鎖定畫面等檢視畫面。這個頁面不包含以下內容:
- 針對原始設備製造商可在視窗中新增的內容建立限制。
- 強制您採用本頁所述的抽象概念。
總覽
您可以使用 SystemUIOverlayWindow
管理系統,顯示法律聲明、全螢幕使用者切換器、後視鏡頭、空調控制項和鍵盤保護程式等畫面。這個視窗位於應用程式空間之外,可讓您控管檢視畫面的 Z 順序、顯示/隱藏觸發事件,以及整體自訂設定,包括檢視畫面位置、大小、透明度和顏色。同時,您也不必擔心系統資訊列或其他系統 UI 物件的狀態,因為在隱藏或顯示相應檢視畫面時,這些物件會自動隱藏或顯示。
如要充分利用 SystemUIOverlayWindow
,請為檢視器仲介建立檢視控制器。中介者會傳遞至視窗的全域狀態控制器。這些 ViewMediator 有以下功能:
- 協調檢視控制器之間的作業。
- 容納檢視控制器的商業邏輯。
檢視控制器 (由檢視中介服務協調):
- 擁有自己的檢視畫面。
- 建立
OverlayViewsMediator
可附加商業邏輯的 setter。 - 建立檢視畫面的顯示和隱藏動畫。
SystemUIOverlayWindow
Manager 是 SystemUI 元件,可做為進入點,將中介者初始化並註冊至全域狀態控制器,而全域狀態控制器會連結至檢視控制器,讓中介者可直接呼叫檢視控制器,在視窗中顯示及隱藏檢視畫面。
OverlayViewController
OverlayViewController
負責顯示在 SystemUIOverlayWindow
中的檢視畫面,並控制檢視畫面的顯示和隱藏方式。它還可讓您附加必要的事件監聽器,以便將事件監聽器連結至商業邏輯。
重要方法簽章
/**
* Owns a {@link View} that is present in SystemUIOverlayWindow
.
*/
public class OverlayViewController {
/**
* Shows content of {@link OverlayViewController}.
*
* Should be used to show view externally and in particular by {@link OverlayViewMediator}.
*/
public final void start();
/**
* Hides content of {@link OverlayViewController}.
*
* Should be used to hide view externally and in particular by {@link OverlayViewMediator}.
*/
public final void stop();
/**
* Inflate layout owned by controller.
*/
public final void inflate(ViewGroup baseLayout);
/**
* Called once inflate finishes.
*/
protected void onFinishInflate();
/**
* Returns {@code true} if layout owned by controller has been inflated.
*/
public final boolean isInflated();
/**
* Subclasses should override this method to implement reveal animations and implement logic
* specific to when the layout owned by the controller is shown.
*
* Should only be overridden by Superclass but not called by any {@link OverlayViewMediator}.
*/
protected void showInternal();
/**
* Subclasses should override this method to implement conceal animations and implement logic
* specific to when the layout owned by the controller is hidden.
*
* Should only be overridden by Superclass but not called by any {@link OverlayViewMediator}.
*/
protected void hideInternal();
/**
* Provides access to layout owned by controller.
*/
protected final View getLayout();
/** Returns the {@link OverlayViewGlobalStateController}. */
protected final OverlayViewGlobalStateController getOverlayViewGlobalStateController();
/** Returns whether the view controlled by this controller is visible. */
public final boolean isVisible();
/**
* Returns the ID of the focus area that should receive focus when this view is the
* topmost view or {@link View#NO_ID} if there is no focus area.
*/
@IdRes
protected int getFocusAreaViewId();
/** Returns whether the view controlled by this controller has rotary focus. */
protected final boolean hasRotaryFocus();
/**
* Sets whether this view allows rotary focus. This should be set to {@code true} for the
* topmost layer in the overlay window and {@code false} for the others.
*/
public void setAllowRotaryFocus(boolean allowRotaryFocus);
/**
* Refreshes the rotary focus in this view if we are in rotary mode. If the view already has
* rotary focus, it leaves the focus alone. Returns {@code true} if a new view was focused.
*/
public boolean refreshRotaryFocusIfNeeded();
/**
* Returns {@code true} if heads up notifications should be displayed over this view.
*/
protected boolean shouldShowHUN();
/**
* Returns {@code true} if navigation bar insets should be displayed over this view. Has no
* effect if {@link #shouldFocusWindow} returns {@code false}.
*/
protected boolean shouldShowNavigationBarInsets();
/**
* Returns {@code true} if status bar insets should be displayed over this view. Has no
* effect if {@link #shouldFocusWindow} returns {@code false}.
*/
protected boolean shouldShowStatusBarInsets();
/**
* Returns {@code true} if this view should be hidden during the occluded state.
*/
protected boolean shouldShowWhenOccluded();
/**
* Returns {@code true} if the window should be focued when this view is visible. Note that
* returning {@code false} here means that {@link #shouldShowStatusBarInsets} and
* {@link #shouldShowNavigationBarInsets} will have no effect.
*/
protected boolean shouldFocusWindow();
/**
* Returns {@code true} if the window should use stable insets. Using stable insets means that
* even when system bars are temporarily not visible, inset from the system bars will still be
* applied.
*
* NOTE: When system bars are hidden in transient mode, insets from them will not be applied
* even when the system bars become visible. Setting the return value to {@true} here can
* prevent the OverlayView from overlapping with the system bars when that happens.
*/
protected boolean shouldUseStableInsets();
/**
* Returns the insets types to fit to the sysui overlay window when this
* {@link OverlayViewController} is in the foreground.
*/
@WindowInsets.Type.InsetsType
protected int getInsetTypesToFit();
/**
* Optionally returns the sides of enabled system bar insets to fit to the sysui overlay window
* when this {@link OverlayViewController} is in the foreground.
*
* For example, if the bottom and left system bars are enabled and this method returns
* WindowInsets.Side.LEFT, then the inset from the bottom system bar will be ignored.
*
* NOTE: By default, this method returns {@link #INVALID_INSET_SIDE}, so insets to fit are
* defined by {@link #getInsetTypesToFit()}, and not by this method, unless it is overridden
* by subclasses.
*
* NOTE: {@link #NO_INSET_SIDE} signifies no insets from any system bars will be honored. Each
* {@link OverlayViewController} can first take this value and add sides of the system bar
* insets to honor to it.
*
* NOTE: If getInsetSidesToFit is overridden to return {@link WindowInsets.Side}, it always
* takes precedence over {@link #getInsetTypesToFit()}. That is, the return value of {@link
* #getInsetTypesToFit()} will be ignored.
*/
@WindowInsets.Side.InsetsSide
protected int getInsetSidesToFit();
}
OverlayPanelViewController
OverlayPanelViewController
控制器會擴充 OverlayViewController
,並為其超類別提供額外的拖曳動畫功能。
OverlayViewMediator
OverlayViewMediator
會納入可顯示或隱藏多個 OverlayViewController
例項的商業邏輯,因此也能以某種方式管理檢視控制項之間的協調。
/** * Controls when to show and hide {@link OverlayViewController}(s). */ public interface OverlayViewMediator { /** * Register listeners that could use ContentVisibilityAdjuster to show/hide content. * * Note that we do not unregister listeners because SystemUI components are expected to live * for the lifecycle of the device. */ void registerListeners(); /** * Allows for post-inflation callbacks and listeners to be set inside required {@link * OverlayViewController}(s). */ void setupOverlayContentViewControllers(); }
SystemUIOverlayWindowManager
SystemUIOverlayWindowManager
負責擔任 SystemUI 物件,為 SystemUIOverlayWindow
管理系統提供一個入口點,以便初始化並將 OverlayViewMediator
例項註冊至 OverlayViewGlobalStateController
。

OverlayViewGlobalStateController
OverlayViewGlobalStateController
會接收來自 OverlayViewController
執行個體的呼叫,以便顯示或隱藏自身。因此,它也會保留 SystemUIOverlayWindow
中顯示或隱藏的狀態。
節目觀看流程如下所示:

隱藏檢視流程
隱藏檢視畫面流程如下所示:

公用方法簽章
公開方法簽名會以以下方式編碼:
/**
* This controller is responsible for the following:
* <p><ul>
* <li>Holds the global state for SystemUIOverlayWindow.
* <li>Allows {@link SystemUIOverlayWindowManager} to register {@link OverlayViewMediator}(s).
* <li>Enables {@link OverlayViewController)(s) to reveal/conceal themselves while respecting the
* global state of SystemUIOverlayWindow.
* </ul>
*/
@SysUISingleton
public class OverlayViewGlobalStateController {
/**
* Register {@link OverlayViewMediator} to use in SystemUIOverlayWindow
.
*/
public void registerMediator(OverlayViewMediator overlayViewMediator);
/**
* Show content in Overlay Window using {@link OverlayPanelViewController}.
*
* This calls {@link OverlayViewGlobalStateController#showView(OverlayViewController, Runnable)}
* where the runnable is nullified since the actual showing of the panel is handled by the
* controller itself.
*/
public void showView(OverlayPanelViewController panelViewController);
/**
* Show content in Overlay Window using {@link OverlayViewController}.
*/
public void showView(OverlayViewController viewController, @Nullable Runnable show);
/**
* Hide content in Overlay Window using {@link OverlayPanelViewController}.
*
* This calls {@link OverlayViewGlobalStateController#hideView(OverlayViewController, Runnable)}
* where the runnable is nullified since the actual hiding of the panel is handled by the
* controller itself.
*/
public void hideView(OverlayPanelViewController panelViewController);
/**
* Hide content in Overlay Window using {@link OverlayViewController}.
*/
public void hideView(OverlayViewController viewController, @Nullable Runnable hide);
/** Returns {@code true} is the window is visible. */
public boolean isWindowVisible();
/**
* Sets the {@link android.view.WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM} flag of the
* sysui overlay window.
*/
public void setWindowNeedsInput(boolean needsInput);
/** Returns {@code true} if the window is focusable. */
public boolean isWindowFocusable();
/** Sets the focusable flag of the sysui overlawy window. */
public void setWindowFocusable(boolean focusable);
/** Inflates the view controlled by the given view controller. */
public void inflateView(OverlayViewController viewController);
/**
* Return {@code true} if OverlayWindow is in a state where HUNs should be displayed above it.
*/
public boolean shouldShowHUN();
/**
* Set the OverlayViewWindow to be in occluded or unoccluded state. When OverlayViewWindow is
* occluded, all views mounted to it that are not configured to be shown during occlusion will
* be hidden.
*/
public void setOccluded(boolean occluded);
}
如何將檢視畫面新增至 SysUIOverlayWindow
詳情請參閱Codelab。
步驟 1:將 ViewStub 新增至 SysUIOverlayWindow
步驟 2:建立 OverlayViewController
使用新的 ViewStub
建立新的可插入 OverlayViewController
。
步驟 3:OverlayViewMediator
建立新的可插入 OverlayViewMediator
,或使用現有的 OverlayViewMediator
(略過步驟 4),然後註冊事件監聽器,以便隱藏或顯示新的 OverlayViewController
。
步驟 4:設定新的 OverlayViewMediator
將新的 OverlayViewMediator
新增至
OverlayWindowModule
和 config_carSystemUIOverlayViewsMediator
。
注意事項
當 SysUIPrimaryWindow
覆蓋整個畫面時,窗格下方的任何元素都不會註冊觸控事件。因此,如果視窗覆蓋整個畫面,但其內容會留下一些負空間,您可以選擇將負空間模糊處理,並將事件監聽器附加至該空間,以便關閉視窗中的內容。