WindowManager
是一个接口类,继承自ViewManager
,ViewManager
中定义了3个方法,分布用来添加、更新和删除View,如下所示:
public interface ViewManager {
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
WindowManager
也继承了这些方法,而这些方法传入的参数都是View类型,说明了Window
是以View的形式存在的。
3.具体实现
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&