WindowManager.LayoutParams详解总结和对应实例
public static class
WindowManager.LayoutParams
extends?ViewGroup.LayoutParamsimplements?Parcelable
| java.lang.Object | ||
| ???? | android.view.ViewGroup.LayoutParams | |
| ? | ???? | android.view.WindowManager.LayoutParams |
| int | FLAGS_CHANGED | 用于表示flags發(fā)生了變化,關(guān)于此的詳細(xì)內(nèi)容請(qǐng)看后文。 |
| int | FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | Window flag: as long as this window is visible to the user, allow the lock screen to activate while the screen is on. 當(dāng)該window對(duì)用戶可見的時(shí)候,允許鎖屏。 |
| int | FLAG_ALT_FOCUSABLE_IM | Window flag: invert the state of?FLAG_NOT_FOCUSABLE?with respect to how this window interacts with the current method. |
| int | FLAG_BLUR_BEHIND | Window flag: blur everything behind this window. 讓該window后所有東西都模糊(blur) |
| int | FLAG_DIM_BEHIND | Window flag: everything behind this window will be dimmed. 讓該window后所有的東西都成暗淡(dim) |
| int | FLAG_DISMISS_KEYGUARD | Window flag: when set the window will cause the keyguard to be dismissed,? only if it is not a secure lock keyguard. |
| int | FLAG_DITHER | Window flag: turn on dithering when compositing this window to the screen. 開啟抖動(dòng)(dithering) |
| int | FLAG_FORCE_NOT_FULLSCREEN | Window flag: Override {@link #FLAG_FULLSCREEN and force the screen decorations (such as status bar) to be shown. 恢復(fù)window非全屏顯示 |
| int | FLAG_FULLSCREEN | Window flag: Hide all screen decorations (e.g. 讓window進(jìn)行全屏顯示 |
| int | FLAG_HARDWARE_ACCELERATED | Indicates whether this window should be hardware accelerated. 對(duì)該window進(jìn)行硬件加速. 該flag必須在設(shè)置你的Activity或Dialog的Content View之前進(jìn)行設(shè)置, 而且如果你在mainfest文件中用Android:hardwareAccelerated開啟了該屬性的話,那么你在程序中就不能再改變它。mainfest文件中android:hardwareAccelerated屬性默認(rèn)是開啟的("true")。 |
| int | FLAG_IGNORE_CHEEK_PRESSES | Window flag: intended for windows that will often be used when the user is holding the screen against their face, it will aggressively filter the event stream to prevent unintended presses in this situation that may not be desired for a particular window, when such an event stream is detected, the application will receive a CANCEL motion event to indicate this so applications can handle this accordingly by taking no action on the event until the finger is released. |
| int | FLAG_KEEP_SCREEN_ON | Window flag: as long as this window is visible to the user, keep the device's screen turned on and bright. 當(dāng)該window對(duì)用戶可見時(shí),讓設(shè)備屏幕處于高亮(bright)狀態(tài)。 |
| int | FLAG_LAYOUT_INSET_DECOR | Window flag: a special option only for use in combination with?FLAG_LAYOUT_IN_SCREEN. |
| int | FLAG_LAYOUT_IN_SCREEN | Window flag: place the window within the entire screen, ignoring decorations around the border (a.k.a. 讓window占滿整個(gè)手機(jī)屏幕,不留任何邊界(border) |
| int | FLAG_LAYOUT_NO_LIMITS | Window flag: allow window to extend outside of the screen. window大小不再不受手機(jī)屏幕大小限制,即window可能超出屏幕之外,這時(shí)部分內(nèi)容在屏幕之外。 |
| int | FLAG_NOT_FOCUSABLE | Window flag: this window won't ever get key input focus, so the user can not send key or other button events to it. 讓window不能獲得焦點(diǎn),這樣用戶快就不能向該window發(fā)送按鍵事件及按鈕事件 |
| int | FLAG_NOT_TOUCHABLE | Window flag: this window can never receive touch events. 讓該window不接受觸摸屏事件 |
| int | FLAG_NOT_TOUCH_MODAL | Window flag: Even when this window is focusable (its {@link #FLAG_NOT_FOCUSABLE is not set),? allow any pointer events outside of the window to be sent to the windows behind it. 即使在該window在可獲得焦點(diǎn)情況下,仍然把該window之外的任何event發(fā)送到該window之后的其他window. |
| int | FLAG_SCALED | Window flag: a special mode where the layout parameters are used to perform scaling of the surface when it is composited to the screen. |
| int | FLAG_SECURE | Window flag: don't allow screen shots while this window is displayed. 當(dāng)該window在進(jìn)行顯示的時(shí)候,不允許截屏。 |
| int | FLAG_SHOW_WALLPAPER | Window flag: ask that the system wallpaper be shown behind your window. 在該window后顯示系統(tǒng)的墻紙(wallpaper) |
| int | FLAG_SHOW_WHEN_LOCKED | Window flag: special flag to let windows be shown when the screen is locked. 當(dāng)鎖屏的時(shí)候,顯示該window. |
| int | FLAG_SPLIT_TOUCH | Window flag: when set the window will accept for touch events outside of its bounds to be sent to other windows that also support split touch. When this flag is not set, the first pointer that goes down determines the window to which all subsequent touches go until all pointers go up. When this flag is set, each pointer (not necessarily the first) that goes down determines the window to which all subsequent touches of that pointer will go until that pointer goes up thereby enabling touches with multiple pointers to be split across multiple windows 當(dāng)該window在可以接受觸摸屏情況下,讓因在該window之外,而發(fā)送到后面的window的觸摸屏可以支持split touch. |
| int | FLAG_TOUCHABLE_WHEN_WAKING | Window flag: When set, if the device is asleep when the touch screen is pressed, you will receive this first touch event. 當(dāng)手機(jī)處于睡眠狀態(tài)時(shí),如果屏幕被按下,那么該window將第一個(gè)收到到事件 |
| int | FLAG_TURN_SCREEN_ON | Window flag: when set as a window is being added or made visible, once the window has been shown then the system will poke the power manager's user activity (as if the user had woken up the device) to turn the screen on. 當(dāng)然window被顯示的時(shí)候,系統(tǒng)將把它當(dāng)做一個(gè)用戶活動(dòng)事件,以點(diǎn)亮手機(jī)屏幕。 |
| int | FLAG_WATCH_OUTSIDE_TOUCH | Window flag: if you have set?FLAG_NOT_TOUCH_MODAL, you can set this flag to receive a single special MotionEvent with the action?MotionEvent.ACTION_OUTSIDE? for touches that occur outside of your window. 如果你設(shè)置了該flag,那么在你FLAG_NOT_TOUNCH_MODAL的情況下,即使觸摸屏事件發(fā)送在該window之外,其事件被發(fā)送到了后面的window,那么該window仍然將以MotionEvent.ACTION_OUTSIDE形式收到該觸摸屏事件 |
| int | SOFT_INPUT_ADJUST_NOTHING | Adjustment option for?softInputMode: set to have a window not adjust for a shown input method. | |
| int | SOFT_INPUT_ADJUST_PAN ? ?????????????? | Adjustment option for?softInputMode: set to have a window pan when an input method is shown, so it doesn't need to deal with resizing but just panned by the framework to ensure the current input focus is visible. 即使調(diào)整空白區(qū)域,軟鍵盤還是有可能遮擋一些有內(nèi)容區(qū)域, 這時(shí)用戶就只有退出軟鍵盤才能看到這些被遮擋區(qū)域并進(jìn)行交互。 ? ???????????????? | |
| int | SOFT_INPUT_ADJUST_RESIZE | Adjustment option for?softInputMode: set to allow the window to be resized when an input method is shown, so that its contents are not covered by the input method. 這樣的話控件可能會(huì)變形。 | |
| int | SOFT_INPUT_ADJUST_UNSPECIFIED | Adjustment option for?softInputMode: nothing specified. | |
| int | SOFT_INPUT_IS_FORWARD_NAVIGATION | Bit for?softInputMode: set when the user has navigated forward to the window. | |
| int | SOFT_INPUT_MASK_ADJUST | Mask for?softInputMode?of the bits that determine the way that the window should be adjusted to accommodate the soft input window. SOFT_INPUT_ADJUST_NOTHING, SOFT_INPUT_ADJUST_PAN, SOFT_INPUT_ADJUST_RESIZE, SOFT_INPUT_ADJUST_UNSPECIFIED。 SOFT_INPUT_STATE_ALWAYS_HIDDEN, SOFT_INPUT_STATE_ALWAYS_VISIBLE, SOFT_INPUT_STATE_VISIBLE, SOFT_INPUT_STATE_UNSPECIFIED之一 | |
| int | SOFT_INPUT_MASK_STATE | Mask for?softInputMode?of the bits that determine the desired visibility state of the soft input area for this window. | |
| int | SOFT_INPUT_MODE_CHANGED | 用于表示softInputMode發(fā)生了變化。關(guān)于此的詳細(xì)內(nèi)容請(qǐng)看后文。 | |
| int | SOFT_INPUT_STATE_ALWAYS_HIDDEN | Visibility state for?softInputMode:? focus. | |
| int | SOFT_INPUT_STATE_ALWAYS_VISIBLE | Visibility state for?softInputMode: receives input focus. | |
| int | SOFT_INPUT_STATE_HIDDEN | Visibility state for?softInputMode:? the user is navigating forward to your window). | |
| int | SOFT_INPUT_STATE_UNCHANGED | Visibility state for?softInputMode: please don't change the state of the soft input area. | |
| int | SOFT_INPUT_STATE_UNSPECIFIED | Visibility state for?softInputMode: no state has been specified. | |
| int | SOFT_INPUT_STATE_VISIBLE | Visibility state for?softInputMode: please show the soft input area when normally appropriate? | |
Window type系列 該系列主要用于表示window的類型。我們可以通過WindowManager.LayoutParams的type變量直接進(jìn)行設(shè)置.
| int | TYPE_APPLICATION | Window type: a normal application window. 普通的應(yīng)用程序window,token必須設(shè)置為Activity的token,以指出該窗口屬誰 |
| int | TYPE_APPLICATION_ATTACHED_DIALOG | Window type: like?TYPE_APPLICATION_PANEL, but layout of the window happens as that of a top-level window,?not?as a child of its container. 對(duì)話框。類似于面板窗口,繪制類似于頂層窗口,而不是宿主的子窗口。 |
| int | TYPE_APPLICATION_MEDIA | Window type: window for showing media (e.g. 媒體窗口,例如視頻。顯示于宿主窗口下層。 |
| int | TYPE_APPLICATION_PANEL | Window type: a panel on top of an application window. 面板窗口,顯示于宿主窗口上層 |
| int | TYPE_APPLICATION_STARTING | Window type: special application window that is displayed while the application is starting. 用于應(yīng)用程序啟動(dòng)時(shí)所顯示的窗口。應(yīng)用本身不要使用這種類型。它用于讓系統(tǒng)顯示些信息,直到應(yīng)用程序可以開啟自己的窗口 |
| int | TYPE_APPLICATION_SUB_PANEL | Window type: a sub-panel on top of an application window. 應(yīng)用程序窗口的子面板。顯示于所有面板窗口的上層。(GUI的一般規(guī)律,越“子”越靠上) |
| int | TYPE_BASE_APPLICATION | Window type: an application window that serves as the "base" window of the overall application; all other application windows will appear on top of it. 所有程序窗口的“基地”窗口,其他應(yīng)用程序窗口都顯示在它上面。? |
| int | TYPE_CHANGED | 表示window的類型發(fā)生了變化,關(guān)于此的詳細(xì)內(nèi)容請(qǐng)看后文。 |
| int | TYPE_INPUT_METHOD | Window type: internal input methods windows, which appear above the normal UI. 內(nèi)部輸入法窗口,顯示于普通UI之上。應(yīng)用程序可重新布局以免被此窗口覆蓋 |
| int | TYPE_INPUT_METHOD_DIALOG | Window type: internal input methods dialog windows, which appear above the current input method window. 內(nèi)部輸入法對(duì)話框,顯示于當(dāng)前輸入法窗口之上 |
| int | TYPE_KEYGUARD | Window type: keyguard window. 鎖屏窗口 |
| int | TYPE_KEYGUARD_DIALOG | Window type: dialogs that the keyguard shows 鎖屏?xí)r顯示的對(duì)話框 |
| int | TYPE_PHONE | Window type: phone. 電話窗口。它用于電話交互(特別是呼入)。它置于所有應(yīng)用程序之上,狀態(tài)欄之下。 |
| int | TYPE_PRIORITY_PHONE | Window type: priority phone UI, which needs to be displayed even if the keyguard is active. 電話優(yōu)先,當(dāng)鎖屏?xí)r顯示。此窗口不能獲得輸入焦點(diǎn),否則影響鎖屏。 |
| int | TYPE_SEARCH_BAR | Window type: the search bar. 搜索欄。只能有一個(gè)搜索欄;它位于屏幕上方。 |
| int | TYPE_STATUS_BAR | Window type: the status bar. 狀態(tài)欄類型的window。只能有一個(gè)狀態(tài)欄window;它位于屏幕頂端,其他窗口都位于它下方。 |
| int | TYPE_STATUS_BAR_PANEL | Window type: panel that slides out from over the status bar 狀態(tài)欄的滑動(dòng)面板 |
| int | TYPE_SYSTEM_ALERT | Window type: system window, such as low power alert. 系統(tǒng)提示window,比如電池低的警告。它總是出現(xiàn)在應(yīng)用程序窗口之上。 |
| int | TYPE_SYSTEM_DIALOG | Window type: panel that slides out from the status bar 系統(tǒng)對(duì)話框。(例如音量調(diào)節(jié)框) |
| int | TYPE_SYSTEM_ERROR | Window type: internal system error windows, appear on top of everything they can. 系統(tǒng)內(nèi)部錯(cuò)誤提示,顯示于所有內(nèi)容之上 |
| int | TYPE_SYSTEM_OVERLAY | Window type: system overlay windows, which need to be displayed on top of everything else. 系統(tǒng)頂層窗口。顯示在其他一切內(nèi)容之上。此窗口不能獲得輸入焦點(diǎn),否則影響鎖屏。 |
| int | TYPE_TOAST | Window type: transient notifications. toast類型的window |
| int | TYPE_WALLPAPER | Window type: wallpaper window,? placed behind any window that wants to sit on top of the wallpaper. 用于墻紙的window |
| int | FIRST_APPLICATION_WINDOW | Start of window types that represent normal application windows. Constant Value: 1 (0x00000001) |
| int | FIRST_SUB_WINDOW | Start of types of sub-windows. Constant Value: 1000 (0x000003e8) |
| int | FIRST_SYSTEM_WINDOW | Start of system-specific window types. Constant Value: 2000 (0x000007d0) |
| int | LAST_APPLICATION_WINDOW | End of types of application windows. Constant Value: 99 (0x00000063) |
| int | LAST_SUB_WINDOW | End of types of sub-windows. Constant Value: 1999 (0x000007cf) |
| int | LAST_SYSTEM_WINDOW | End of types of system windows. Constant Value: 2999 (0x00000bb7) |
| float | BRIGHTNESS_OVERRIDE_FULL | Value for?screenBrightness?and?buttonBrightness? indicating that the screen or button backlight brightness should be set to the hightest value? when this window is in front. 把brightness(screenBrightness/buttonBrightness)設(shè)置到最高值。 |
| float | BRIGHTNESS_OVERRIDE_NONE | Default value for?screenBrightness?and?buttonBrightness?indicating that the brightness value is not overridden for this window and normal brightness policy should be used. 不對(duì)brightness(screenBrightness/buttonBrightness)重新進(jìn)行設(shè)置,采用默認(rèn)的普通值。 |
| float | BRIGHTNESS_OVERRIDE_OFF | Value for?screenBrightness?and?buttonBrightness?indicating that the screen or button backlight brightness should be set to the lowest value when this window is in front. 把brightness(screenBrightness/buttonBrightness)設(shè)置到最低值。 |
| int | ALPHA_CHANGED | 用于表示成員變量alpha是否被改變 |
| int | ANIMATION_CHANGED | 用于表示成員變量windowAnimations是否被改變 |
| int | DIM_AMOUNT_CHANGED | 用于表示成員變量dimAmount是否被改變 |
| int | FLAGS_CHANGED | 用于表示成員變量flags是否被改變 |
| int | LAYOUT_CHANGED | 用于表示layout是否被改變.這里的layout是指以下變量所包含的信息: width,height,x,y,?verticalMargin,verticalWeight,horizontalMargin,horizontalWeight |
| int | SCREEN_BRIGHTNESS_CHANGED | 用于表示brightness是否被改變. 這里的brightness是指以下變量對(duì)應(yīng)的信息:screenBrightness,buttonBrightness |
| int | SCREEN_ORIENTATION_CHANGED | 用于表示成員變量screenOrientation是否被改變 |
| int | SOFT_INPUT_MODE_CHANGED | 用于表示成員變量softInputMode是否被改變 |
| int | TITLE_CHANGED | 用于表示成員變量title是否被改變 |
| int | TYPE_CHANGED | 用于表示成員變量type是否被改變 |
| int | FORMAT_CHANGED | Constant Value:?8?(0x00000008) 用于表示成員變量format是否被改變 |
| public int | height | Information about how tall the view wants to be. |
| public?LayoutAnimationController.AnimationParameters | layoutAnimationParameters | Used to animate layouts. |
| public int | width | Information about how wide the view wants to be. |
| public static final?Creator<WindowManager.LayoutParams> | CREATOR | ? |
| public float | alpha | An alpha value to apply to this entire window. |
| public float | buttonBrightness | This can be used to override the standard behavior of the button and keyboard backlights. |
| public float | dimAmount | When?FLAG_DIM_BEHIND?is set, this is the amount of dimming to apply. |
| public int | flags | Various behavioral options/flags. |
| public int | format | The desired bitmap format. |
| public int | gravity | Placement of window within the screen as per?Gravity. |
| public float | horizontalMargin | The horizontal margin, as a percentage of the container's width, between the container and the widget. |
| public float | horizontalWeight | Indicates how much of the extra space will be allocated horizontally to the view associated with these LayoutParams. |
| public int | memoryType | This field is deprecated. this is ignored |
| public?String | packageName | Name of the package owning this window. |
| public float | screenBrightness | This can be used to override the user's preferred brightness of the screen. |
| public int | screenOrientation | Specific orientation value for a window. |
| public int | softInputMode | Desired operating mode for any soft input area. |
| public int | systemUiVisibility | Control the visibility of the status bar. |
| public?IBinder | token | Identifier for this window. |
| public int | type | The general type of window. |
| public float | verticalMargin | The vertical margin, as a percentage of the container's height, between the container and the widget. |
| public float | verticalWeight | Indicates how much of the extra space will be allocated vertically to the view associated with these LayoutParams. |
| public int | windowAnimations | A style resource defining the animations to use for this window. |
| public int | x | X position for this window. |
| public int | y | Y position for this window. |
| final int | copyFrom(WindowManager.LayoutParams?o) |
| String | debug(String?output)Returns a String representation of this set of layout parameters. |
| int | describeContents()Describe the kinds of special objects contained in this Parcelable's marshalled representation. |
| final?CharSequence | getTitle() |
| static boolean | mayUseInputMethod(int flags)Given a particular set of window manager flags, determine whether such a window may be a target for an input method when it has focus. |
| final void | setTitle(CharSequence?title) |
| String | toString()Returns a string containing a concise, human-readable description of this object. |
| void | writeToParcel(Parcel?out, int parcelableFlags)Flatten this object in to a Parcel. |
Android懸浮窗實(shí)現(xiàn) 使用WindowManager
?
WindowManager介紹
通過Context.getSystemService(Context.WINDOW_SERVICE)可以獲得?WindowManager對(duì)象。
每一個(gè)WindowManager對(duì)象都和一個(gè)特定的?Display綁定。
想要獲取一個(gè)不同的display的WindowManager,可以用?createDisplayContext(Display)來獲取那個(gè)display的?Context,之后再使用:
Context.getSystemService(Context.WINDOW_SERVICE)來獲取WindowManager。
?
使用WindowManager可以在其他應(yīng)用最上層,甚至手機(jī)桌面最上層顯示窗口。
調(diào)用的是WindowManager繼承自基類的addView方法和removeView方法來顯示和隱藏窗口。具體見后面的實(shí)例。
?
另:API 17推出了Presentation,它將自動(dòng)獲取display的Context和WindowManager,可以方便地在另一個(gè)display上顯示窗口。
?
WindowManager實(shí)現(xiàn)懸浮窗例子
聲明權(quán)限
首先在manifest中添加如下權(quán)限:
<!-- 顯示頂層浮窗 --> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
注意:在MIUI上需要在設(shè)置中打開本應(yīng)用的”顯示懸浮窗”開關(guān),并且重啟應(yīng)用,否則懸浮窗只能顯示在本應(yīng)用界面內(nèi),不能顯示在手機(jī)桌面上。
?
服務(wù)獲取和基本參數(shù)設(shè)置
// 獲取應(yīng)用的ContextmContext = context.getApplicationContext();// 獲取WindowManagermWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);參數(shù)設(shè)置:
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();// 類型params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT// 設(shè)置flagint flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 如果設(shè)置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View收不到Back鍵的事件params.flags = flags;// 不設(shè)置這個(gè)彈出框的透明遮罩顯示為黑色params.format = PixelFormat.TRANSLUCENT;// FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到后面的窗口// 設(shè)置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時(shí),后面的應(yīng)用圖標(biāo)由不可長(zhǎng)按變?yōu)榭砷L(zhǎng)按// 不設(shè)置這個(gè)flag的話,home頁(yè)的劃屏?xí)袉栴} params.width = LayoutParams.MATCH_PARENT;params.height = LayoutParams.MATCH_PARENT;params.gravity = Gravity.CENTER;?
點(diǎn)擊和按鍵事件
除了View中的各個(gè)控件的點(diǎn)擊事件之外,彈窗View的消失控制需要一些處理。
點(diǎn)擊彈窗外部可隱藏彈窗的效果,首先,懸浮窗是全屏的,只不過最外層的是透明或者半透明的:
布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center"android:background="@color/darken_background"android:gravity="center"android:orientation="vertical" ><RelativeLayoutandroid:id="@+id/popup_window"android:layout_width="@dimen/dialog_window_width"android:layout_height="@dimen/dialog_window_height"android:background="@color/white"android:orientation="vertical" ><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="@dimen/dialog_title_height"android:gravity="center"android:text="@string/default_title"android:textColor="@color/dialog_title_text_color"android:textSize="@dimen/dialog_title_text_size" /><Viewandroid:id="@+id/title_divider"android:layout_width="match_parent"android:layout_height="2dp"android:layout_below="@id/title"android:background="@drawable/dialog_title_divider" /><TextViewandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/title_divider"android:gravity="center"android:padding="@dimen/dialog_content_padding_side"android:text="@string/default_content"android:textColor="@color/dialog_content_text_color"android:textSize="@dimen/dialog_content_text_size" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:orientation="horizontal"android:paddingBottom="@dimen/dialog_content_padding_bottom"android:paddingLeft="@dimen/dialog_content_padding_side"android:paddingRight="@dimen/dialog_content_padding_side" ><Buttonandroid:id="@+id/negativeBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/promote_window_negative_btn_selector"android:focusable="true"android:padding="@dimen/dialog_button_padding"android:text="@string/default_btn_cancel"android:textColor="@color/dialog_negative_btn_text_color"android:textSize="@dimen/dialog_button_text_size" /><Buttonandroid:id="@+id/positiveBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="18dp"android:layout_weight="1"android:background="@drawable/promote_window_positive_btn_selector"android:focusable="true"android:padding="@dimen/dialog_button_padding"android:text="@string/default_btn_ok"android:textColor="@color/dialog_positive_btn_text_color"android:textSize="@dimen/dialog_button_text_size" /></LinearLayout></RelativeLayout></LinearLayout>?
點(diǎn)擊外部可消除設(shè)置:
// 點(diǎn)擊窗口外部區(qū)域可消除// 這點(diǎn)的實(shí)現(xiàn)主要將懸浮窗設(shè)置為全屏大小,外層有個(gè)透明背景,中間一部分視為內(nèi)容區(qū)域// 所以點(diǎn)擊內(nèi)容區(qū)域外部視為點(diǎn)擊懸浮窗外部final View popupWindowView = view.findViewById(R.id.popup_window);// 非透明的內(nèi)容區(qū)域 view.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {LogUtil.i(LOG_TAG, "onTouch");int x = (int) event.getX();int y = (int) event.getY();Rect rect = new Rect();popupWindowView.getGlobalVisibleRect(rect);if (!rect.contains(x, y)) {WindowUtils.hidePopupWindow();}LogUtil.i(LOG_TAG, "onTouch : " + x + ", " + y + ", rect: "+ rect);return false;}});?
點(diǎn)擊Back鍵可隱藏彈窗:
注意Flag不能設(shè)置WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE。
// 點(diǎn)擊back鍵可消除view.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_BACK:WindowUtils.hidePopupWindow();return true;default:return false;}}});?
完整效果
完整代碼:
package com.example.hellowindow;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;public class MainActivity extends Activity {private Handler mHandler = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mHandler = new Handler();Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mHandler.postDelayed(new Runnable() {@Overridepublic void run() {WindowUtils.showPopupWindow(MainActivity.this);}}, 1000 * 3);}});} } package com.example.hellowindow;import android.content.Context; import android.graphics.PixelFormat; import android.graphics.Rect; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnKeyListener; import android.view.View.OnTouchListener; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.WindowManager.LayoutParams; import android.widget.Button;/*** 彈窗輔助類** @ClassName WindowUtils***/ public class WindowUtils {private static final String LOG_TAG = "WindowUtils";private static View mView = null;private static WindowManager mWindowManager = null;private static Context mContext = null;public static Boolean isShown = false;/*** 顯示彈出框** @param context* @param view*/public static void showPopupWindow(final Context context) {if (isShown) {LogUtil.i(LOG_TAG, "return cause already shown");return;}isShown = true;LogUtil.i(LOG_TAG, "showPopupWindow");// 獲取應(yīng)用的ContextmContext = context.getApplicationContext();// 獲取WindowManagermWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);mView = setUpView(context);final WindowManager.LayoutParams params = new WindowManager.LayoutParams();// 類型params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT// 設(shè)置flagint flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 如果設(shè)置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View收不到Back鍵的事件params.flags = flags;// 不設(shè)置這個(gè)彈出框的透明遮罩顯示為黑色params.format = PixelFormat.TRANSLUCENT;// FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到后面的窗口// 設(shè)置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時(shí),后面的應(yīng)用圖標(biāo)由不可長(zhǎng)按變?yōu)榭砷L(zhǎng)按// 不設(shè)置這個(gè)flag的話,home頁(yè)的劃屏?xí)袉栴} params.width = LayoutParams.MATCH_PARENT;params.height = LayoutParams.MATCH_PARENT;params.gravity = Gravity.CENTER;mWindowManager.addView(mView, params);LogUtil.i(LOG_TAG, "add view");}/*** 隱藏彈出框*/public static void hidePopupWindow() {LogUtil.i(LOG_TAG, "hide " + isShown + ", " + mView);if (isShown && null != mView) {LogUtil.i(LOG_TAG, "hidePopupWindow");mWindowManager.removeView(mView);isShown = false;}}private static View setUpView(final Context context) {LogUtil.i(LOG_TAG, "setUp view");View view = LayoutInflater.from(context).inflate(R.layout.popupwindow,null);Button positiveBtn = (Button) view.findViewById(R.id.positiveBtn);positiveBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {LogUtil.i(LOG_TAG, "ok on click");// 打開安裝包// 隱藏彈窗 WindowUtils.hidePopupWindow();}});Button negativeBtn = (Button) view.findViewById(R.id.negativeBtn);negativeBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {LogUtil.i(LOG_TAG, "cancel on click");WindowUtils.hidePopupWindow();}});// 點(diǎn)擊窗口外部區(qū)域可消除// 這點(diǎn)的實(shí)現(xiàn)主要將懸浮窗設(shè)置為全屏大小,外層有個(gè)透明背景,中間一部分視為內(nèi)容區(qū)域// 所以點(diǎn)擊內(nèi)容區(qū)域外部視為點(diǎn)擊懸浮窗外部final View popupWindowView = view.findViewById(R.id.popup_window);// 非透明的內(nèi)容區(qū)域 view.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {LogUtil.i(LOG_TAG, "onTouch");int x = (int) event.getX();int y = (int) event.getY();Rect rect = new Rect();popupWindowView.getGlobalVisibleRect(rect);if (!rect.contains(x, y)) {WindowUtils.hidePopupWindow();}LogUtil.i(LOG_TAG, "onTouch : " + x + ", " + y + ", rect: "+ rect);return false;}});// 點(diǎn)擊back鍵可消除view.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_BACK:WindowUtils.hidePopupWindow();return true;default:return false;}}});return view;} }?
?
總結(jié)
以上是生活随笔為你收集整理的WindowManager.LayoutParams详解总结和对应实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 路由器停产,360的其他硬件产品是否安好
- 下一篇: \t\t终身受用!告诉你关于PDF文件的