美文网首页
Android添加夜间模式---add一层view

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

作者: suniney | 来源:发表于2019-02-25 17:16 被阅读0次
在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>

相关文章

网友评论

      本文标题:Android添加夜间模式---add一层view

      本文链接:https://www.haomeiwen.com/subject/dqigyqtx.html