WindowManager.LayoutParams详解总结和对应实例
来源:互联网 发布:全球经济数据库 市盈率 编辑:程序博客网 时间:2024/06/10 13:39
WindowManager.LayoutParams
extends ViewGroup.LayoutParamsimplements Parcelable
当该window对用户可见的时候,允许锁屏。intFLAG_ALT_FOCUSABLE_IMWindow flag: invert the state of
FLAG_NOT_FOCUSABLE
with respect to how this window interacts with the current method.intFLAG_BLUR_BEHINDWindow flag: blur everything behind this window.让该window后所有东西都模糊(blur)intFLAG_DIM_BEHINDWindow flag: everything behind this window will be dimmed.
让该window后所有的东西都成暗淡(dim)intFLAG_DISMISS_KEYGUARDWindow flag: when set the window will cause the keyguard to be dismissed,
only if it is not a secure lock keyguard.intFLAG_DITHERWindow flag: turn on dithering when compositing this window to the screen.
开启抖动(dithering)intFLAG_FORCE_NOT_FULLSCREENWindow flag: Override {@link #FLAG_FULLSCREEN and force the screen decorations (such as status bar) to be shown.
恢复window非全屏显示intFLAG_FULLSCREENWindow flag: Hide all screen decorations (e.g.
让window进行全屏显示intFLAG_HARDWARE_ACCELERATED
Indicates whether this window should be hardware accelerated.
对该window进行硬件加速.
该flag必须在设置你的Activity或Dialog的Content View之前进行设置,
而且如果你在mainfest文件中用Android:hardwareAccelerated开启了该属性的话,那么你在程序中就不能再改变它。mainfest文件中android:hardwareAccelerated属性默认是开启的("true")。
intFLAG_IGNORE_CHEEK_PRESSESWindow 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.intFLAG_KEEP_SCREEN_ONWindow flag: as long as this window is visible to the user, keep the device's screen turned on and bright.当该window对用户可见时,让设备屏幕处于高亮(bright)状态。intFLAG_LAYOUT_INSET_DECORWindow flag: a special option only for use in combination with
FLAG_LAYOUT_IN_SCREEN
.intFLAG_LAYOUT_IN_SCREENWindow flag: place the window within the entire screen, ignoring decorations around the border (a.k.a.让window占满整个手机屏幕,不留任何边界(border)intFLAG_LAYOUT_NO_LIMITSWindow flag: allow window to extend outside of the screen.
window大小不再不受手机屏幕大小限制,即window可能超出屏幕之外,这时部分内容在屏幕之外。intFLAG_NOT_FOCUSABLEWindow flag: this window won't ever get key input focus, so the user can not send key or other button events to it.
让window不能获得焦点,这样用户快就不能向该window发送按键事件及按钮事件intFLAG_NOT_TOUCHABLEWindow flag: this window can never receive touch events.
让该window不接受触摸屏事件intFLAG_NOT_TOUCH_MODALWindow 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在可获得焦点情况下,仍然把该window之外的任何event发送到该window之后的其他window.intFLAG_SCALEDWindow flag: a special mode where the layout parameters are used to perform scaling of the surface when it is composited to the screen.intFLAG_SECUREWindow flag: don't allow screen shots while this window is displayed.
当该window在进行显示的时候,不允许截屏。intFLAG_SHOW_WALLPAPERWindow flag: ask that the system wallpaper be shown behind your window.
在该window后显示系统的墙纸(wallpaper)intFLAG_SHOW_WHEN_LOCKEDWindow flag: special flag to let windows be shown when the screen is locked.
当锁屏的时候,显示该window.intFLAG_SPLIT_TOUCHWindow 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
当该window在可以接受触摸屏情况下,让因在该window之外,而发送到后面的window的触摸屏可以支持split touch.intFLAG_TOUCHABLE_WHEN_WAKINGWindow flag: When set, if the device is asleep when the touch screen is pressed, you will receive this first touch event.
当手机处于睡眠状态时,如果屏幕被按下,那么该window将第一个收到到事件intFLAG_TURN_SCREEN_ONWindow 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.
当然window被显示的时候,系统将把它当做一个用户活动事件,以点亮手机屏幕。intFLAG_WATCH_OUTSIDE_TOUCHWindow 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.
如果你设置了该flag,那么在你FLAG_NOT_TOUNCH_MODAL的情况下,即使触摸屏事件发送在该window之外,其事件被发送到了后面的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.
当显示软键盘时,不调整window的布局
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.
当显示软键盘时,调整window的空白区域来显示软键盘。
即使调整空白区域,软键盘还是有可能遮挡一些有内容区域,
这时用户就只有退出软键盘才能看到这些被遮挡区域并进行交互。
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.
当显示软键盘时,调整window内的控件大小以便显示软键盘。
这样的话控件可能会变形。
int
SOFT_INPUT_ADJUST_UNSPECIFIED
Adjustment option for softInputMode: nothing specified.
不指定显示软件盘时,window的调整方式。
int
SOFT_INPUT_IS_FORWARD_NAVIGATION
Bit for softInputMode: set when the user has navigated
forward to the window.
表示用户导航(navigate)到了你的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.
显示软键盘时,用于表示window调整方式的bite的mask。
显示软键盘时的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_HIDDEN,
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.
用于描述软键盘显示规则的bite的mask.
int
SOFT_INPUT_MODE_CHANGED
用于表示softInputMode发生了变化。关于此的详细内容请看后文。
int
SOFT_INPUT_STATE_ALWAYS_HIDDEN
Visibility state for softInputMode:
please always hide any soft input area when this window receives
focus.
总是隐藏软键盘。
int
SOFT_INPUT_STATE_ALWAYS_VISIBLE
Visibility state for softInputMode:
please always make the soft input area visible when this window
receives input focus.
总是显示软键盘
int
SOFT_INPUT_STATE_HIDDEN
Visibility state for softInputMode:
please hide any soft input area when normally appropriate (when
the user is navigating forward to your window).
用户导航(navigate)到你的窗口的时候,隐藏软键盘
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
(when the user is navigating forward to your window).
用户导航(navigate)到你的窗口的时候,显示软键盘
普通的应用程序window,token必须设置为Activity的token,以指出该窗口属谁intTYPE_APPLICATION_ATTACHED_DIALOGWindow 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.对话框。类似于面板窗口,绘制类似于顶层窗口,而不是宿主的子窗口。intTYPE_APPLICATION_MEDIAWindow type: window for showing media (e.g.
媒体窗口,例如视频。显示于宿主窗口下层。intTYPE_APPLICATION_PANELWindow type: a panel on top of an application window.
面板窗口,显示于宿主窗口上层intTYPE_APPLICATION_STARTINGWindow type: special application window that is displayed while the application is starting.
用于应用程序启动时所显示的窗口。应用本身不要使用这种类型。它用于让系统显示些信息,直到应用程序可以开启自己的窗口intTYPE_APPLICATION_SUB_PANELWindow type: a sub-panel on top of an application window.
应用程序窗口的子面板。显示于所有面板窗口的上层。(GUI的一般规律,越“子”越靠上)intTYPE_BASE_APPLICATIONWindow type: an application window that serves as the "base" window of the overall application; all other application windows will appear on top of it.
所有程序窗口的“基地”窗口,其他应用程序窗口都显示在它上面。 intTYPE_CHANGED表示window的类型发生了变化,关于此的详细内容请看后文。intTYPE_INPUT_METHODWindow type: internal input methods windows, which appear above the normal UI.
内部输入法窗口,显示于普通UI之上。应用程序可重新布局以免被此窗口覆盖intTYPE_INPUT_METHOD_DIALOGWindow type: internal input methods dialog windows, which appear above the current input method window.
内部输入法对话框,显示于当前输入法窗口之上intTYPE_KEYGUARDWindow type: keyguard window.
锁屏窗口intTYPE_KEYGUARD_DIALOGWindow type: dialogs that the keyguard shows
锁屏时显示的对话框intTYPE_PHONEWindow type: phone.
电话窗口。它用于电话交互(特别是呼入)。它置于所有应用程序之上,状态栏之下。intTYPE_PRIORITY_PHONEWindow type: priority phone UI, which needs to be displayed even if the keyguard is active.
电话优先,当锁屏时显示。此窗口不能获得输入焦点,否则影响锁屏。intTYPE_SEARCH_BARWindow type: the search bar.
搜索栏。只能有一个搜索栏;它位于屏幕上方。intTYPE_STATUS_BARWindow type: the status bar.
状态栏类型的window。只能有一个状态栏window;它位于屏幕顶端,其他窗口都位于它下方。intTYPE_STATUS_BAR_PANELWindow type: panel that slides out from over the status bar
状态栏的滑动面板intTYPE_SYSTEM_ALERTWindow type: system window, such as low power alert.
系统提示window,比如电池低的警告。它总是出现在应用程序窗口之上。intTYPE_SYSTEM_DIALOGWindow type: panel that slides out from the status bar
系统对话框。(例如音量调节框)intTYPE_SYSTEM_ERRORWindow type: internal system error windows, appear on top of everything they can.
系统内部错误提示,显示于所有内容之上intTYPE_SYSTEM_OVERLAYWindow type: system overlay windows, which need to be displayed on top of everything else.
系统顶层窗口。显示在其他一切内容之上。此窗口不能获得输入焦点,否则影响锁屏。intTYPE_TOASTWindow type: transient notifications.
toast类型的windowintTYPE_WALLPAPERWindow type: wallpaper window,
placed behind any window that wants to sit on top of the wallpaper.
用于墙纸的window
Constant Value: 1 (0x00000001)intFIRST_SUB_WINDOWStart of types of sub-windows.
Constant Value: 1000 (0x000003e8)intFIRST_SYSTEM_WINDOWStart of system-specific window types.
Constant Value: 2000 (0x000007d0)
Constant Value: 99 (0x00000063)intLAST_SUB_WINDOWEnd of types of sub-windows.
Constant Value: 1999 (0x000007cf)intLAST_SYSTEM_WINDOWEnd of types of system windows.
Constant Value: 2999 (0x00000bb7)
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)设置到最高值。floatBRIGHTNESS_OVERRIDE_NONEDefault value for
screenBrightness
and buttonBrightness
indicating that the brightness value is not overridden for this window and normal brightness policy should be used.不对brightness(screenBrightness/buttonBrightness)重新进行设置,采用默认的普通值。floatBRIGHTNESS_OVERRIDE_OFFValue 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)设置到最低值。
width,height,x,y, verticalMargin,verticalWeight,horizontalMargin,horizontalWeightintSCREEN_BRIGHTNESS_CHANGED用于表示brightness是否被改变.
这里的brightness是指以下变量对应的信息:screenBrightness,buttonBrightnessintSCREEN_ORIENTATION_CHANGED用于表示成员变量screenOrientation是否被改变intSOFT_INPUT_MODE_CHANGED用于表示成员变量softInputMode是否被改变intTITLE_CHANGED用于表示成员变量title是否被改变intTYPE_CHANGED用于表示成员变量type是否被改变intFORMAT_CHANGEDConstant Value: 8 (0x00000008)
用于表示成员变量format是否被改变
FLAG_DIM_BEHIND
is set, this is the amount of dimming to apply.Gravity
.Android悬浮窗实现 使用WindowManager
WindowManager介绍
通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得 WindowManager
对象。
每一个WindowManager对象都和一个特定的 Display
绑定。
想要获取一个不同的display的WindowManager,可以用 createDisplayContext(Display)
来获取那个display的 Context
,之后再使用:
Context.getSystemService(Context.WINDOW_SERVICE)来获取WindowManager。
使用WindowManager可以在其他应用最上层,甚至手机桌面最上层显示窗口。
调用的是WindowManager继承自基类的addView方法和removeView方法来显示和隐藏窗口。具体见后面的实例。
另:API 17推出了Presentation
,它将自动获取display的Context和WindowManager,可以方便地在另一个display上显示窗口。
WindowManager实现悬浮窗例子
声明权限
首先在manifest中添加如下权限:
<!-- 显示顶层浮窗 --><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
注意:在MIUI上需要在设置中打开本应用的”显示悬浮窗”开关,并且重启应用,否则悬浮窗只能显示在本应用界面内,不能显示在手机桌面上。
服务获取和基本参数设置
// 获取应用的Context mContext = context.getApplicationContext(); // 获取WindowManager mWindowManager = (WindowManager) mContext .getSystemService(Context.WINDOW_SERVICE);
参数设置:
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(); // 类型 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // WindowManager.LayoutParams.TYPE_SYSTEM_ALERT // 设置flag int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件 params.flags = flags; // 不设置这个弹出框的透明遮罩显示为黑色 params.format = PixelFormat.TRANSLUCENT; // FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口 // 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按 // 不设置这个flag的话,home页的划屏会有问题 params.width = LayoutParams.MATCH_PARENT; params.height = LayoutParams.MATCH_PARENT; params.gravity = Gravity.CENTER;
点击和按键事件
除了View中的各个控件的点击事件之外,弹窗View的消失控制需要一些处理。
点击弹窗外部可隐藏弹窗的效果,首先,悬浮窗是全屏的,只不过最外层的是透明或者半透明的:
布局如下:
<?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" > <RelativeLayout android: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" > <TextView android: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" /> <View android:id="@+id/title_divider" android:layout_width="match_parent" android:layout_height="2dp" android:layout_below="@id/title" android:background="@drawable/dialog_title_divider" /> <TextView android: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" /> <LinearLayout android: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" > <Button android: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" /> <Button android: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>
点击外部可消除设置:
// 点击窗口外部区域可消除 // 这点的实现主要将悬浮窗设置为全屏大小,外层有个透明背景,中间一部分视为内容区域 // 所以点击内容区域外部视为点击悬浮窗外部 final View popupWindowView = view.findViewById(R.id.popup_window);// 非透明的内容区域 view.setOnTouchListener(new OnTouchListener() { @Override public 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; } });
点击Back键可隐藏弹窗:
注意Flag不能设置WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE。
// 点击back键可消除 view.setOnKeyListener(new OnKeyListener() { @Override public 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; @Override protected 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() { @Override public void onClick(View v) { mHandler.postDelayed(new Runnable() { @Override public 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"); // 获取应用的Context mContext = context.getApplicationContext(); // 获取WindowManager mWindowManager = (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 // 设置flag int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件 params.flags = flags; // 不设置这个弹出框的透明遮罩显示为黑色 params.format = PixelFormat.TRANSLUCENT; // FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口 // 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按 // 不设置这个flag的话,home页的划屏会有问题 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() { @Override public 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() { @Override public void onClick(View v) { LogUtil.i(LOG_TAG, "cancel on click"); WindowUtils.hidePopupWindow(); } }); // 点击窗口外部区域可消除 // 这点的实现主要将悬浮窗设置为全屏大小,外层有个透明背景,中间一部分视为内容区域 // 所以点击内容区域外部视为点击悬浮窗外部 final View popupWindowView = view.findViewById(R.id.popup_window);// 非透明的内容区域 view.setOnTouchListener(new OnTouchListener() { @Override public 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; } }); // 点击back键可消除 view.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: WindowUtils.hidePopupWindow(); return true; default: return false; } } }); return view; }}
- WindowManager.LayoutParams详解总结和对应实例
- android分析windowManager,和WindowManager.LayoutParams类总结
- WindowManager.LayoutParams详解
- WindowManager.LayoutParams详解
- WindowManager.LayoutParams详解
- WindowManager.LayoutParams详解
- android WindowManager.LayoutParams详解
- WindowManager.LayoutParams详解
- WindowManager.LayoutParams详解
- WindowManager.LayoutParams详解
- WindowManager.LayoutParams详解
- WindowManager.LayoutParams 详解
- WindowManager.LayoutParams 详解
- WindowManager.LayoutParams 详解
- WindowManager.LayoutParams类详解
- WindowManager.LayoutParams 详解
- WindowManager.LayoutParams 详解
- WindowManager.LayoutParams详解
- poj2418 Map统计单词数
- VC++中的字体设置方法详解
- 【CRM】01- WEB UI 下拉列表
- IIC协议
- Unity 3D
- WindowManager.LayoutParams详解总结和对应实例
- 哈密顿图 poj 1776
- 存储过程例子
- java缓存技术
- java-final关键字
- Linux C 读取文件夹下所有文件(包括子文件夹)的文件名(转载)
- 多条件查询--使用dapper命令参数动态拼接出最安全的sql语句
- 前端代码书写规范
- hdu 2047简单递推公式