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 token 傳入 WindowManager#addView(View, LayoutParams)android.view.WindowManager.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 呼。

請注意,針對 UI 相關查詢使用 android.app.Applicationandroid.app.Service 內容可能會導致具有可變螢幕大小的裝置上的版面配置或持續性問題,例如可折迭 () 或多視窗模式,因為這些非 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 檔。

此頁面的部分是根據 原始碼專案所建立和共用的工作進行修改,並根據中所述的詞彙使用。

適用於