您的位置:首页 > 其它

页面添加蒙版,但是不影响页面其他事件响应

2016-07-15 17:55 357 查看
最近需要做一个比较坑人的功能,就是在APP每个页面上都添加一个蒙版的效果,但是还不能影响页面上其他的事件,要是仅仅修改几个页面,应该相对比较容易的,因为可以修改xml,在最外层套一个FrameLayout或者RelativeLayout,然后将蒙版的View添加到底部,focusable和clickable都设置成”false”即可。相关xml布局如下(使用FrameLayout,注蒙版的View一定要添加在下面)

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();


以上就是实现在页面添加蒙版,但是不影响页面其他事件响应这个功能的相关代码,希望对大家有帮助,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: