Context.CreateWindowContext 方法

定义

重载

名称 说明
CreateWindowContext(Int32, Bundle)

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

CreateWindowContext(Display, Int32, Bundle)

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

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

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

返回

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

属性

注解

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

窗口上下文是可用于添加非活动窗口的上下文,例如 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#Rno-op。

下面是将现有令牌附加到窗口上下文:</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 回调。

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

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

本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。

适用于

CreateWindowContext(Display, Int32, Bundle)

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

[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

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

返回

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

属性

注解

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

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

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

本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。

适用于