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

<?xml version="1.0" encoding="utf-8"?>
   <View xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/nightmode"
     style="@style/night_mode_style" />
<!-- 夜间模式 -->
   <style name="night_mode_style">
       <item name="android:layout_width">match_parent</item>
       <item name="android:layout_height">match_parent</item>
       <item name="android:background">#b2000000</item>
   </style>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容