3.1浮窗布局
悬浮窗的简易布局如下的可参考下面的layout_floating_window.xml
文件。顶层深色部分的FrameLayout
布局是用来实现悬浮窗的拖拽功能的,点击右上角ImageView
可以实现关闭悬浮窗,剩下区域显示内容,这里只是简单地显示文本内容,不做复杂的东西,故只设置TextView。
<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”>
<FrameLayout
android:id="@+id/layout_drag"
android:layout_width=“match_parent”
android:layout_height=“15dp”
android:background="#dddddd">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_close"
android:layout_width=“15dp”
android:layout_height=“15dp”
android:layout_gravity=“end”
android:src="@drawable/img_delete"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_content"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_gravity=“center_horizontal”
android:background="#eeeeee"
android:scrollbars=“vertical”/>
3.2 悬浮窗的实现
1. 使用服务Service
Service
是一种可在后台执行长时间运行操作而不提供界面的应用组件,可由其他应用组件启动,而且即使用户切换到其他应用,仍将在后台继续运行。要保证应用在后台时,悬浮窗仍然可以正常显示,所以这里可以使用Service<