Android巧用DecorView实现对话框功能
程序员文章站
2022-10-24 17:06:25
如果还不知道decorview,那也没有什么关系 ^_^
先来看看实现的效果
实现的大致思路
首先需要明白什么是decorview,他是andro...
如果还不知道decorview,那也没有什么关系 ^_^
先来看看实现的效果
实现的大致思路
- 首先需要明白什么是decorview,他是android中界面的根布局。其实android的activity界面整个就是一个控件树,decorview是根节点,decorview的孩子节点就是一个linearlayout,这个linearlayout的孩子系节点就包括状态栏 + 和我们自己写的布局
- decorview是framlayout的子类(可以从源码中看到)
- 既然decorview是根节点,而且还是framelayout,所以我们可以把我们自己的布局 添加到decorview 或者 从decorview移除,这样就模拟出了一个dialog的效果~~ ,当然这个dialog的样式,动画就可以自己想怎么写就怎么写了撒
- 通过activity.getwindow().getdecorview()可以获得decorview
[下面大量 代码 ]
第一个对话框的实现
public class tipsdialog { private activity activity; private view rootview; private textview confirmtextview; private textview canceltextview; private textview contenttextview; private boolean isshowing; public tipsdialog(activity activity) { this.activity = activity; isshowing = false; rootview = layoutinflater.from(activity).inflate(r.layout.view_tips_dialog,null); confirmtextview = (textview) rootview.findviewbyid(r.id.view_tips_dialog_tv_confirm); canceltextview = (textview) rootview.findviewbyid(r.id.view_tips_dialog_tv_cancel); contenttextview = (textview) rootview.findviewbyid(r.id.view_tips_dialog_tv_content); } public void show(){ if(activity == null){ return; } if(isshowing){ return; } viewgroup decorview = (viewgroup) activity.getwindow().getdecorview(); framelayout.layoutparams params = new framelayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); params.gravity = gravity.center; rootview.setlayoutparams(params); decorview.addview(rootview); rootview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dismiss(); } }); rotateanimation rotateanimation = new rotateanimation(0,720f,rotateanimation.relative_to_self,0.5f,rotateanimation.relative_to_self,0.5f); rotateanimation.setduration(2000); contenttextview.startanimation(rotateanimation); isshowing = true; } public void dismiss(){ if(!isshowing){ return; } isshowing = false; if(rootview.getparent() == null){ return; } viewgroup parent = (viewgroup) rootview.getparent(); parent.removeview(rootview); } public int getrandomcolor(){ random random = new random(); return color.argb(random.nextint(200),random.nextint(240),random.nextint(240),random.nextint(240)); } public boolean isshowing() { return isshowing; } }
其实就是show的时候将布局添加到decorview上面去,dismiss的时候将布局从decorview上面移除
提示的实现(没有处理完善~~ 仅仅就是说明哈decorview)
public class toptipdialog { private activity activity; private view rootview; private boolean isshowing; private static final int view_height = 64;//px public toptipdialog(activity activity) { this.activity = activity; rootview = layoutinflater.from(activity).inflate(r.layout.view_top_tip_dialog,null); } public void show(){ if(isshowing){ return; } viewgroup decorview = (viewgroup) activity.getwindow().getdecorview(); framelayout.layoutparams params = new framelayout.layoutparams(viewgroup.layoutparams.match_parent, view_height); params.gravity = gravity.top; params.setmargins(0,0,0,-view_height); rootview.setlayoutparams(params); translateanimation translateanimation = new translateanimation(0,0,-view_height,0); translateanimation.setduration(1500); translateanimation.setfillafter(true); decorview.addview(rootview); rootview.startanimation(translateanimation); rootview.postdelayed(new runnable() { @override public void run() { translateanimation translateanimation1 = new translateanimation(0,0,0,-view_height); translateanimation1.setduration(1500); translateanimation1.setfillafter(true); rootview.startanimation(translateanimation1); } },3000); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Android二维码创建实例