Context.CreateWindowContext 方法

定义

重载

CreateWindowContext(Int32, Bundle)

为非活动窗口创建上下文。

CreateWindowContext(Display, Int32, Bundle)

Context为给定 Display上的非android.app.Activity activity窗口创建 。

CreateWindowContext(Int32, Bundle)

为非活动窗口创建上下文。

[Android.Runtime.Register("createWindowContext", "(ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_ILandroid_os_Bundle_Handler", ApiSince=30)]
public virtual Android.Content.Context CreateWindowContext (int type, Android.OS.Bundle? options);
[<Android.Runtime.Register("createWindowContext", "(ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_ILandroid_os_Bundle_Handler", ApiSince=30)>]
abstract member CreateWindowContext : int * Android.OS.Bundle -> Android.Content.Context
override this.CreateWindowContext : int * Android.OS.Bundle -> Android.Content.Context

参数

type
Int32

中的窗口类型 WindowManager.LayoutParams

options
Bundle

用于传递窗口相关选项的捆绑包

返回

Context可用于创建非窗口的android.app.Activity activity

属性

注解

为非活动窗口创建上下文。

窗口上下文是可用于添加非活动窗口的上下文,例如 android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY。 必须从具有关联 Display的上下文(例如 android.app.Activity Activity 或使用 创建的上下文)创建 #createDisplayContext(Display)窗口上下文。

创建窗口上下文时,使用适合 Configuration 使用它创建的窗口可以占用的显示区域;当视图时 android.view.LayoutInflater inflating 必须使用它,以便可以使用适当的 Resources来扩充它们。

下面是用于在>主显示器上添加应用程序覆盖窗口的示例代码<:</b>

...
            final DisplayManager dm = anyContext.getSystemService(DisplayManager.class);
            final Display primaryDisplay = dm.getDisplay(DEFAULT_DISPLAY);
            final Context windowContext = anyContext.createDisplayContext(primaryDisplay)
                    .createWindowContext(TYPE_APPLICATION_OVERLAY, null);
            final View overlayView = Inflater.from(windowContext).inflate(someLayoutXml, null);

            // WindowManager.LayoutParams initialization
            ...
            // The types used in addView and createWindowContext must match.
            mParams.type = TYPE_APPLICATION_OVERLAY;
            ...

            windowContext.getSystemService(WindowManager.class).addView(overlayView, mParams);

此上下文的配置和资源将调整为显示区域,其中将添加具有提供类型的窗口。 <b>请注意,与同一上下文关联的所有窗口都将具有相关性,并且只能在显示器上的不同显示器或区域之间一起移动。</b> 如果需要添加不同的窗口类型或非关联的窗口,则应使用单独的上下文。

创建窗口上下文是一项成本高昂的操作。 滥用此 API 可能会导致性能大幅下降。 最佳做法是尽可能使用相同的窗口上下文。 一种方法是创建一个具有特定窗口类型的窗口上下文并显示,并在需要它的任何位置使用它。

在 之后Build.VERSION_CODES#S,窗口上下文通过重写android.view.WindowManager.LayoutParams#token tokenandroid.view.WindowManager.LayoutParams传入 WindowManager#addView(View, LayoutParams)的 来接收现有令牌的配置更改。 当应用程序需要将其窗口附加到用于窗口令牌共享用例的现有活动时,此功能非常有用。

请注意,中的 Build.VERSION_CODES#R 窗口上下文没有此功能。 这是中 Build.VERSION_CODES#R窗口上下文的无操作。

下面是将<>现有令牌附加到窗口上下文的示例代码:</b>

final DisplayManager dm = anyContext.getSystemService(DisplayManager.class);
            final Display primaryDisplay = dm.getDisplay(DEFAULT_DISPLAY);
            final Context windowContext = anyContext.createWindowContext(primaryDisplay,
                    TYPE_APPLICATION, null);

            // Get an existing token.
            final IBinder existingToken = activity.getWindow().getAttributes().token;

            // The types used in addView() and createWindowContext() must match.
            final WindowManager.LayoutParams params = new WindowManager.LayoutParams(TYPE_APPLICATION);
            params.token = existingToken;

            // After WindowManager#addView(), the server side will extract the provided token from
            // LayoutParams#token (existingToken in the sample code), and switch to propagate
            // configuration changes from the node associated with the provided token.
            windowContext.getSystemService(WindowManager.class).addView(overlayView, mParams);

在 之后Build.VERSION_CODES#S,窗口上下文提供通过调用 #registerComponentCallbacks(ComponentCallbacks)来侦听其Configuration更改的功能,而其他类型的 Context 会将 注册ComponentCallbacks#getApplicationContext() its Application context。 请注意,窗口上下文仅传播 ComponentCallbacks#onConfigurationChanged(Configuration) 回调。 ComponentCallbacks#onLowMemory() 不会调用 或 中的 ComponentCallbacks2 其他回调。

请注意,使用 android.app.Applicationandroid.app.Service 上下文进行 UI 相关查询可能会导致屏幕大小 (可变的设备上出现布局或连续性问题,例如可折叠) 或多窗口模式,因为这些非 UI 上下文可能不会反映 Configuration 视觉对象容器的更改。

android.content.Context.createWindowContext(int, android.os.Bundle)Java 文档。

此页面的部分内容是基于 创建和共享的工作进行的修改,并根据 署名许可中所述的术语使用。

适用于

CreateWindowContext(Display, Int32, Bundle)

Context为给定 Display上的非android.app.Activity activity窗口创建 。

[Android.Runtime.Register("createWindowContext", "(Landroid/view/Display;ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_Landroid_view_Display_ILandroid_os_Bundle_Handler", ApiSince=31)]
public virtual Android.Content.Context CreateWindowContext (Android.Views.Display display, int type, Android.OS.Bundle? options);
[<Android.Runtime.Register("createWindowContext", "(Landroid/view/Display;ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_Landroid_view_Display_ILandroid_os_Bundle_Handler", ApiSince=31)>]
abstract member CreateWindowContext : Android.Views.Display * int * Android.OS.Bundle -> Android.Content.Context
override this.CreateWindowContext : Android.Views.Display * int * Android.OS.Bundle -> Android.Content.Context

参数

display
Display

Display要与之关联的

type
Int32

中的窗口类型 WindowManager.LayoutParams

options
Bundle

用于传递与窗口相关的选项的捆绑包。

返回

Context可用于创建非窗口的android.app.Activity activity

属性

注解

Context为给定 Display上的非android.app.Activity activity窗口创建 。

类似于 #createWindowContext(int, Bundle),但 display 会传入 ,而不是隐式使用 #getDisplay() original Context's Display

android.content.Context.createWindowContext(android.view.Display, int, android.os.Bundle)Java 文档。

此页面的部分内容是基于 创建和共享的工作进行的修改,并根据 署名许可中所述的术语使用。

适用于