頁面添加蒙版,但是不影響頁面其他事件響應


最近需要做一個比較坑人的功能,就是在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();

以上就是實現在頁面添加蒙版,但是不影響頁面其他事件響應這個功能的相關代碼,希望對大家有幫助,謝謝!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章