Android添加夜间模式---add一层view

在BaseActivity中
 @Override
public void setContentView(int layoutResID) {
       super.setContentView(layoutResID);
       if (CommonPref.getIsNightMode()) {
           addNightView();
       } else {
           removeNightView();
       }
   }
protected void addNightView() {
       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
               LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
       LayoutInflater inflater3 = LayoutInflater.from(this);
        nightView = inflater3.inflate(R.layout.layout_night, null);
       nightView.setLayoutParams(lp);
       ((ViewGroup) getWindow().getDecorView()).addView(nightView);
   }

   protected void removeNightView() {
      if (nightView!=null){
          ((ViewGroup) getWindow().getDecorView()).removeView(nightView);
      }
   }


   

   

你可能感兴趣的:(Android添加夜间模式---add一层view)