悬浮窗实现源码

<style type="text/css">
.layui-layer-shade{z-index:999998;background-color:rgb(0, 0, 0);opacity:0.8;position:fixed;pointer-events:auto;top:0;left:0;width:100%;height:100%}
.mian_aq{position:fixed;top:50px;border-radius:2px;box-shadow: 1px 1px 50px rgba(0,0,0,.3);overflow:hidden;width:800px;border:1px solid #CCC;z-index:999999;background:#fff;left:50%;margin-left:-400px}
.mian_aq .bn{color:#FFF;border:0;cursor:pointer;font-size:18px; position:absolute;right:10px; top:5px;}
</style>
<div class="yc layui-layer-shade"></div>
<div class="yc mian_aq">
  <img src="http://*****/images/*****.jpg" width="800"/>
<span class="bn">×</span>
</div>
<script type="text/javascript">
var cookies = getCookie('yc');
if(cookies==1){
    //$(".yc").hide();
}
$(".bn").on("click", function() {
    //setCookie('yc',1,4);
    $(".yc").hide();
})
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
         }
         if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
         }
     }
    return "";

</script>

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现悬浮窗返回按键功能,可以按照以下步骤进行。 1. 在布局文件中添加一个ImageView控件,并设置返回图标作为其背景。 2. 在代码中定义一个WindowManager对象,用于创建悬浮窗。 3. 在创建悬浮窗时,设置悬浮窗的参数,包括宽度、高度、位置等。 4. 在悬浮窗中添加一个触摸事件监听器,当手指点击返回图标时,关闭悬浮窗。 以下是一个示例代码: //定义WindowManager对象 private WindowManager windowManager; private WindowManager.LayoutParams layoutParams; ... //在onCreate中初始化 windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); layoutParams = new WindowManager.LayoutParams(); //设置悬浮窗类型为系统级别 layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; //设置悬浮窗的宽度和高度 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT; layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置悬浮窗的位置 layoutParams.gravity = Gravity.TOP | Gravity.RIGHT; ... //创建悬浮窗布局 View floatingView = LayoutInflater.from(this).inflate(R.layout.floating_layout, null); //获取返回图标 ImageView backButton = floatingView.findViewById(R.id.back_button); //添加触摸事件监听器 backButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { //关闭悬浮窗 windowManager.removeView(floatingView); } return true; } }); //添加悬浮窗 windowManager.addView(floatingView, layoutParams); 需要注意的是,悬浮窗类型为系统级别时,需要声明SYSTEM_ALERT_WINDOW权限。同时,由于悬浮窗不受Activity生命周期的影响,需要在关闭窗口时手动进行清理,否则可能会引起内存泄漏问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值