Popupwindow背景顏色變灰色,並點擊外部消失

ColorDrawable dw = new ColorDrawable(-00000); //必須放在showatlocation才能起作用,點擊外部消失,但不能使灰色背景色也消失
pw.setBackgroundDrawable(dw);

pw.showAtLocation(rl_parent, Gravity.BOTTOM,0,0);


這一段代碼可以使popupwindow點擊外部消失,必須放在showatlocation才能起作用,點擊外部消失,但不能使灰色背景色也消失。至於爲什麼加了這段代碼點擊外部會消失我也不清楚,希望知道的朋友留言告訴我。


WindowManager.LayoutParams lp=getWindow().getAttributes();
            lp.alpha=0.3f;
            getWindow().setAttributes(lp);


這段代碼使屏幕背景變成灰色


pw.setOnDismissListener(new poponDismissListener()); 

添加消失監聽事件

/** 
     * 添加新筆記時彈出的popWin關閉的事件,主要是爲了將背景透明度改回來 
     * @author cg 
     * 
     */  
    class poponDismissListener implements PopupWindow.OnDismissListener{  
  
        @Override  
        public void onDismiss() {  
        WindowManager.LayoutParams lp=getWindow().getAttributes();
            lp.alpha=1f;
            getWindow().setAttributes(lp);
        }  
          
    }  


還有一個重要的需要補充!:本activity的theme必須使用notitlebar

這是註冊表中註冊activity的

 <activity
            android:name="ivan.weidian.auction.ReleaseAuction"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:exported="true"
            android:theme="@style/NoTitle" >
</activity>

這是在stylt中定義notitle的

<style name="NoTitle" parent="android:Theme.Black.NoTitleBar" />


完整代碼:


case R.id.tv_share_head: //點擊分享按鈕

pw = new PopupWindow(popshare, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pw.setFocusable(true);
ColorDrawable dw = new ColorDrawable(-00000); //必須放在showatlocation才能起作用,點擊外部消失,但不能使灰色背景色也消失
pw.setBackgroundDrawable(dw);
pw.showAtLocation(rl_parent, Gravity.BOTTOM,0,0);
WindowManager.LayoutParams lp=getWindow().getAttributes();
            lp.alpha=0.3f;
            getWindow().setAttributes(lp);
            pw.setOutsideTouchable(true);
            //添加pop窗口關閉事件  
            pw.setOnDismissListener(new poponDismissListener()); 
            
            TextView cancel = (TextView)popshare.findViewById(R.id.tv_cancel);
            cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
WindowManager.LayoutParams lp=getWindow().getAttributes();
           lp.alpha=1f;
           getWindow().setAttributes(lp);
           pw.dismiss();
}
});
break;


/** 
     * 添加新筆記時彈出的popWin關閉的事件,主要是爲了將背景透明度改回來 
     * @author cg 
     * 
     */  
    class poponDismissListener implements PopupWindow.OnDismissListener{  
  
        @Override  
        public void onDismiss() {  
        WindowManager.LayoutParams lp=getWindow().getAttributes();
            lp.alpha=1f;
            getWindow().setAttributes(lp);
        }  
          
    }  

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