一 日夜间模式方法
public class BaseActivity extends Activity {
protected View mCoverView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication app = getApp();
if (app.isNight) {
nightMode();
} else {
dayMode();
}
}
protected void dayMode() {
getApp().isNight=false;
if (mCoverView != null) {
getWindowManager().removeViewImmediate(mCoverView);
}
}
protected void nightMode() {
getApp().isNight=true;
if (mCoverView == null) {
mCoverView = new View(this);
mCoverView.setBackgroundColor(0x80000000);
}
int matchParent = WindowManager.LayoutParams.MATCH_PARENT;
int typeApp = WindowManager.LayoutParams.TYPE_APPLICATION;
int noTouchable = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
int noFocusable = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
int translucent = PixelFormat.TRANSLUCENT;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(matchParent, matchParent, typeApp, noTouchable | noFocusable, translucent);
getWindowManager().addView(mCoverView, params);
}
private MyApplication getApp() {
return ((MyApplication) getApplication());
}
}
二 调用日夜间模式方法
if(i==2){
dayMode();
i=0;
}else if(i==1){
nightMode();
}
三 Application中初始化
private boolean isNight;
android日夜间模式切换
最新推荐文章于 2024-08-12 03:38:04 发布