页面添加蒙版,但是不影响页面其他事件响应
2016-07-15 17:55
357 查看
最近需要做一个比较坑人的功能,就是在APP每个页面上都添加一个蒙版的效果,但是还不能影响页面上其他的事件,要是仅仅修改几个页面,应该相对比较容易的,因为可以修改xml,在最外层套一个FrameLayout或者RelativeLayout,然后将蒙版的View添加到底部,focusable和clickable都设置成”false”即可。相关xml布局如下(使用FrameLayout,注蒙版的View一定要添加在下面)
activity_main.xml
view_mask.xml
但楼主刚刚也提到,需要将项目中所有的页面都添加,几十个啊,根本添加不起,于是就想到能不能封装一下,大家都共用,这样就不用修改xml文件啦,可是我是个小白,不知如何下手,上网搜索资料,蒙版的实现方式都跟我上述实现的方式类似,于是我想到了Dialog这个控件,毕竟实现loading的效果就是使用这个,蒙版效果可以很容易实现,现在的问题就是如何使页面添加蒙版,并不影响页面其他事件响应,于是我就想到了能不能将这个Dialog的focusable和clickable类似的相关属性同样设置为false,于是就自己封装了一个MaskDialog,通过设置窗口布局Flag来实现,以下就是相关代码:
关于WindowManager LayoutParams的所有flag介绍,可参照这篇文章:http://my.oschina.net/u/1781028/blog/307263
MaskDialog的用法,跟Dialog一致,相关代码如下:
以上就是实现在页面添加蒙版,但是不影响页面其他事件响应这个功能的相关代码,希望对大家有帮助,谢谢!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout> <include layout="@layout/view_mask" /> </FrameLayout>
view_mask.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout_mask" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#88000000" android:clickable="false" android:focusable="false" android:gravity="top" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="100dp" android:text="这是蒙版啊!" android:textColor="#fff" /> </LinearLayout>
但楼主刚刚也提到,需要将项目中所有的页面都添加,几十个啊,根本添加不起,于是就想到能不能封装一下,大家都共用,这样就不用修改xml文件啦,可是我是个小白,不知如何下手,上网搜索资料,蒙版的实现方式都跟我上述实现的方式类似,于是我想到了Dialog这个控件,毕竟实现loading的效果就是使用这个,蒙版效果可以很容易实现,现在的问题就是如何使页面添加蒙版,并不影响页面其他事件响应,于是我就想到了能不能将这个Dialog的focusable和clickable类似的相关属性同样设置为false,于是就自己封装了一个MaskDialog,通过设置窗口布局Flag来实现,以下就是相关代码:
public class MaskDialog extends Dialog { private Context context; public MaskDialog(Context context, int theme) { super(context, theme); // TODO Auto-generated constructor stub this.context = context; setupUI(); } protected MaskDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); // TODO Auto-generated constructor stub this.context = context; setupUI(); } public MaskDialog(Context context) { super(context); this.context = context; // TODO Auto-generated constructor stub setupUI(); } private void setupUI() { requestWindowFeature(Window.FEATURE_NO_TITLE); Window window = getWindow(); WindowManager.LayoutParams wl = window.getAttributes(); WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); wl.x = 0; // 这两句设置了对话框的位置. wl.y = 0; wl.width = width; wl.height = height; wl.flags = wl.flags | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; wl.flags = wl.flags | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; window.setAttributes(wl); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.view_mask, null);// 获取弹出页layout setView(view); this.setContentView(view); } private void setView(View view) { TextView txtName = (TextView) view.findViewById(R.id.textView1); txtName.setText("哈哈,这是蒙版页面!"); } }
关于WindowManager LayoutParams的所有flag介绍,可参照这篇文章:http://my.oschina.net/u/1781028/blog/307263
MaskDialog的用法,跟Dialog一致,相关代码如下:
dialog = new MaskDialog(this, android.R.style.Theme_Translucent_NoTitleBar); dialog.show();
以上就是实现在页面添加蒙版,但是不影响页面其他事件响应这个功能的相关代码,希望对大家有帮助,谢谢!
相关文章推荐
- 获取网页URL地址及参数等的两种方法(js和C#)
- mysql授权GRANT ALL PRIVILEGES
- release 包 混淆 Serializable
- Unity vs UE4,虚拟现实开发引擎如何抉择?
- 过滤数组中的数据,避免sql注入、xss等问题
- Adb connection Error:远程主机强迫关闭了一个现有的连接
- RxJava 学习笔记(九) --- Error Handling 错误处理操作
- 菜鸟's problem
- MVC与框架
- Java 23中设计模式之工厂模式
- 【目标识别】深度学习进行目标识别的资源列表
- dll版本冲突的解决方法
- H5内容安全尺寸
- JDBC和JTA事务区别
- 如何让ie6-8识别HTML5新标签
- 近红外摄像头Point-Grey开发日志
- Spring security防止跨站请求伪造(CSRF防护)
- Android界面架构实例之SlidingMenu(1)
- untiy 2D 曲面地图动态生成
- Nginx+Tomcat+Memcached集群