Android悬浮窗的简单实现,已获千赞

本文介绍了如何在Android中实现悬浮窗,包括创建ViewManager接口,设置浮窗布局,利用服务保持后台显示,实现拖拽和关闭功能,以及通过广播进行通信。详细步骤和代码示例展示了整个过程。
摘要由CSDN通过智能技术生成

WindowManager是一个接口类,继承自ViewManagerViewManager中定义了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。

<?xml version="1.0" encoding="utf-8"?>

<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&

  • 25
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值