Context.CreateWindowContext 方法

定義

多載

名稱 Description
CreateWindowContext(Int32, Bundle)

為非活動視窗建立一個上下文。

CreateWindowContext(Display, Int32, Bundle)

為給定Display的非窗口android.app.Activity activity創造 a 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

用於傳遞視窗相關選項的組合

傳回

Context A可以用來創造非視窗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)的 來接收現有令牌的設定變更。 當應用程式需要將其視窗附加到現有活動以進行視窗令牌共享時,這非常有用。

請注意,視窗上下文 in Build.VERSION_CODES#R 沒有這個功能。 這是視窗上下文 Build.VERSION_CODES#R的 no-op。

以下是範例程式碼,用來 <b>將現有的標記附加到視窗上下文 :</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 視覺容器的變更。

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

本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。

適用於

CreateWindowContext(Display, Int32, Bundle)

為給定Display的非窗口android.app.Activity activity創造 a 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

一個用於傳遞視窗相關選項的組合。

傳回

Context A可以用來創造非視窗android.app.Activity activity

屬性

備註

為給定Display的非窗口android.app.Activity activity創造 a Context

類似於 #createWindowContext(int, Bundle),但 是 display 傳遞進去,而非隱含使用 #getDisplay() original Context's Display

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

本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。

適用於