Dialog(九)——利用WindowManager在屏幕任意位置添加Dialog
发布日期:2021-06-30 11:15:17
浏览次数:2
分类:技术文章
本文共 1874 字,大约阅读时间需要 6 分钟。
MainActivity如下:package cn.testalertdialog;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.WindowManager;import android.widget.Button;/** * Demo描述: * 利用WindowManager在屏幕任意位置添加和移除"自定义对话框" * 本质上是使用了WindowManager在屏幕上添加了View,只是 * 我们把这个View做成了常见的对话框的样式 * * 参考资料: * http://blog.csdn.net/lfdfhl/article/details/8195466 * */public class MainActivity extends Activity { private View mDialogView; private WindowManager mWindowManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { mWindowManager = getWindowManager(); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); mDialogView = inflater.inflate(R.layout.dialogview, null); WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.y += 100; params.x += -30; initDialogComponents(mDialogView); //添加对话框 mWindowManager.addView(mDialogView, params); } //初始化自定义对话框中的控件 private void initDialogComponents(View dialogView){ Button firstButton=(Button) dialogView.findViewById(R.id.dialog_firstButton); firstButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("点击了第一个Button"); } }); Button secondButton=(Button) dialogView.findViewById(R.id.dialog_secondButton); secondButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mDialogView!=null) { //移除对话框 mWindowManager.removeView(mDialogView); } } }); }}main.xml如下:
dialogview.xml如下:
转载地址:https://it9527.blog.csdn.net/article/details/28888921 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月12日 19时59分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
基础架构系列篇-系统centos7中DOCKER安装REDIS(已更新)
2019-04-30
基础架构系列篇-系统centos7安装docker+COMPOSE
2019-04-30
基础架构系列篇-系统centos7中docker安装rabbitmq
2019-04-30
基础架构系列篇-NGINX部署VUE
2019-04-30
个人电商项目,基于uni-app+ springcloud +VUE技术
2019-04-30
基础架构系列篇-系统centos7安装kafka
2019-04-30
基础架构系列篇-系统centos7中docker安装分布式文件存储服务minio
2019-04-30
知识点记录-java判断系统是linux或windows
2019-04-30
知识点记录-springboot静态资源映射路径
2019-04-30
知识点记录-vue-cli+webpack打包运行图标显示异常
2019-04-30
知识点记录-springboot2.1集成rabbitmq
2019-04-30
微服务springcloud2系列篇-配置与注册nacos组件
2019-04-30
数据库系列篇mysql8-分库分表中间件mycat(WINDOWS环境)
2019-04-30
用户权限设计-基于RBAC模型
2019-04-30
微服务springcloud2系列篇-网关GATEWAY跨域问题
2019-04-30
基础架构系列篇-系统centos7中docker安装gitlab
2019-04-30
map的几种常用遍历方式
2019-04-30