Android 为应用增加可移动的悬浮窗口
2016-01-23 17:14
621 查看
需求描述:
在使用手机客户端浏览内嵌网页的时候,界面出现一个悬浮的刷新按钮,点击网页重新加载。
布局:
<?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:orientation="vertical" > <ImageView android:id="@+id/float_fresh" android:padding="5dp" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/icon_fresh" android:background="@drawable/bg_floatbutton"/> </LinearLayout>
设置窗口类型在所有窗口之上:
这里说一下这个LayoutParams.TYPE_PHONE。我们看一下官方文档说明:
These are non-application windows providing user interaction with the phone (in particular incoming calls). These windows are normally placed above all applications, but behind the status bar. In multiuser systems shows on all users' windows.
就是说设置了这个属性之后,这个窗口会在所以的界面之上,但是在状态栏的下面。在多用户系统中,所有用户的窗口上都会显示。
定义拖动和点击事件:
mFloatView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub wmParams.x = sWidth - (int) event.getRawX() - mFloatLayout.getMeasuredWidth() / 2; wmParams.y = sHeight - (int) event.getRawY() - mFloatLayout.getMeasuredHeight() / 2; if(wmParams.y > sHeight - titleHeight){ return true; } //刷新 mWindowManager.updateViewLayout(mFloatLayout, wmParams); return false; } }); mFloatView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub web.reload(); } });
别忘了权限声明:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
效果图:
相关文章推荐
- Android问题总结(2015.11-2016.1)一
- android 源码剖析之-------ActivityThread的创建过程
- Android Exception总结
- Android 静默安装
- Android经典的大牛博客推荐
- Android&Java的成长之路之一(java服务器搭建)
- Android开发大牛们的博客地址(持续更新)
- Android系统学习笔记
- android开发工具类总结(一)
- Android 模拟器启动报错:android failed to open framebuffer ( no such file or directory )
- 【Android】Replace "..." with ellipsis character (…, &&;#8230;) ?
- Android 沉浸式状态栏
- (八)Android广播接收器BroadcastReceiver
- Android 开发中常用 ADB 命令总结
- Android 开发中常用 ADB 命令总结
- android实现发送Email功能(从相册中选择图片作为附件)
- Android中的页面切换动画
- android之ListView上拉加载更多和下拉刷新
- android 中获取手机的屏幕的尺寸
- Android创建前台运行的Service