3 Star 1 Fork 0

Gitee 极速下载 / nightmodel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/achenglike/NightModel.git
克隆/下载
README.md 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
like 提交于 2017-12-22 10:32 . 更新readme

NightModel

a night model lib for easy change app's night theme. Based on official night model and need't restart Activity.

中文README

Thanks!

require

  • support appcompat 23.2.0 or above
  • activity extends AppCompatActivity
  • follow Android official nigh model guidance

import

  1. Add it in your root build.gradle at the end of repositories:
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  1. Add the dependency
	dependencies {
	        compile 'com.github.achenglike:NightModel:x.x'
	}

use

  1. set NightModel in Application

    public class AppApplication extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
        NightModelManager.getInstance().init(this);
        ...
    }
    }
    
  2. 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();
    }
    }
  3. 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>
  4. call night/day method

    private void changeNightModel() {
    if (NightModelManager.getInstance().isCurrentNightModel(this)) {
    	NightModelManager.getInstance().applyDayModel(this);
    } else {
    	NightModelManager.getInstance().applyNightModel(this);
     	}
    }

warn

  1. 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.

  2. 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

  3. style:@style/xx just support TextView (android:textColor or android:textSize) now

  4. this lib version 2.1 and before support version 26 and below 26.

  5. this lib version 2.2 and later only support android support lib version 26 and later.

  6. if we set a selector as background for View, night mode not work.

1
https://gitee.com/mirrors/nightmodel.git
git@gitee.com:mirrors/nightmodel.git
mirrors
nightmodel
nightmodel
master

搜索帮助