a night model lib for easy change app's night theme. Based on official night model and need't restart Activity.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.achenglike:NightModel:x.x'
}
set NightModel in Application
public class AppApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
NightModelManager.getInstance().init(this);
...
}
}
attach and detach in Activity which need refresh, not every Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// must before super.onCreate
NightModelManager.getInstance().attach(this);
super.onCreate(savedInstanceState);
}
...
@Override
protected void onDestroy() {
NightModelManager.getInstance().detach(this);
super.onDestroy();
}
}
for stop Activity restart, we need config configChanges="uiMode" in Androidmanifest.xml
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="uiMode"
android:theme="@style/AppTheme.NoActionBar">
</activity>
call night/day method
private void changeNightModel() {
if (NightModelManager.getInstance().isCurrentNightModel(this)) {
NightModelManager.getInstance().applyDayModel(this);
} else {
NightModelManager.getInstance().applyNightModel(this);
}
}
this lib used for resolving the problem that official night model can not refresh started Activities. So, only attach and detach in Activities that need refresh. It's a supplementary lib for official night model.
When create View dynamically in attached Activity with code, you should write like this:
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parentView, false);
because the lib need cache this View, use LayoutInflater.from(MainActivity.this) will use the proxy LayoutInflaterFactory create this View and cache it
style:@style/xx just support TextView (android:textColor or android:textSize) now
this lib version 2.1 and before support version 26 and below 26.
this lib version 2.2 and later only support android support lib version 26 and later.
if we set a selector as background for View, night mode not work.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。