Solve the problem of invalid background transparency of Android popupWindow setting

  • 2021-12-05 07:25:20
  • OfStack

Sometimes when we use popwindow, passing the current activity as View to other fragment will cause us to set the background dimming effect to be invalid. The problem caused by this reason is that when we pass view, the current activity is no longer displayed when popupwindw is popped up, so it will be invalid

Therefore, when setting up, we deal with it like this:


 public void backgroundAlpha(float bgAlpha)
  {

//MApp.getInstance().getMainActivity() Is the main that you want to display when you click activity
    WindowManager.LayoutParams lp = ActivityB.getInstance().getMainActivity().getWindow().getAttributes();
    lp.alpha = bgAlpha; //0.0-1.0
    ActivityB.getInstance().getMainActivity().getWindow().setAttributes(lp);     
    ActivityB.getInstance().getMainActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
  }

That is, activity A is displayed in B as View, then getwindow should be taken from B when setting transparency in A, that is, B. getwindow ();

Additional knowledge: Simple setting of Android and color of blank area of PopupWindow

I won't talk too much, let's just look at the code ~


// Set the background color of white space 
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.alpha = 0.3f;
getActivity().getWindow().setAttributes(lp);
//  Used for PopupWindow Adj. View
View contentView = LayoutInflater.from(getContext()).inflate(R.layout.quote_info_pop, null, false);
//  Create PopupWindow Object, where: 
//  No. 1 1 Parameters are used for PopupWindow In View , No. 2 The parameters are PopupWindow The width of the, 
//  No. 1 3 The parameters are PopupWindow The height of, the first 4 Parameters specify PopupWindow Can you get the focus 
PopupWindow window = new PopupWindow(contentView, getScreenWith() / 3 * 2, (int) getScreenHeight() / 3 * 2, true);
//  Settings PopupWindow Background of 
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.white_shap));
//  Settings PopupWindow Can you respond to external click events 
window.setOutsideTouchable(true);
//  Settings PopupWindow Can you respond to click events 
window.setTouchable(true);
//  Display PopupWindow Of which: 
//  No. 1 1 The parameters are PopupWindow The anchor point, the first 2 And 3 The parameters are PopupWindow Relative to anchor point x , y Migration 
listView = contentView.findViewById(R.id.list);
quoteInfoAdapter = new QuoteInfoAdapter(getActivity(), mList);
listView.setAdapter(quoteInfoAdapter);
window.showAsDropDown(v, getScreenWith() / 6, 0);
//  Or you can call this method to display PopupWindow Of which: 
//  No. 1 1 The parameters are PopupWindow Father of View , No. 2 The parameters are PopupWindow Relative parent View The location of, 
//  No. 1 3 And 4 The parameters are PopupWindow Relative parent View Adj. x , y Migration 
// window.showAtLocation(parent, gravity, x, y);
// Add pop Window closing event 
window.setOnDismissListener(new poponDismissListener());
 Plus monitoring, otherwise, pop When it disappears, transparency will not change back 

class poponDismissListener implements PopupWindow.OnDismissListener {

  @Override
  public void onDismiss() {
    // TODO Auto-generated method stub
    //Log.v("List_noteTypeActivity:", " I am closing the event ");
    WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
    lp.alpha = 1f; //0.0-1.0
    getActivity().getWindow().setAttributes(lp);

  }
}

Related articles: