{"id":13643582,"url":"https://github.com/zhangke3016/ChangeMode","last_synced_at":"2025-04-21T02:30:44.372Z","repository":{"id":201884226,"uuid":"68000881","full_name":"zhangke3016/ChangeMode","owner":"zhangke3016","description":"Implementation of night mode for Android./Android动态切换夜间模式的实现库。","archived":false,"fork":false,"pushed_at":"2016-11-24T12:31:03.000Z","size":3829,"stargazers_count":87,"open_issues_count":3,"forks_count":26,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-09T15:43:01.970Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhangke3016.png","metadata":{"files":{"readme":"README.md","changelog":"changemodelibrary/.gitignore","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-09-12T10:51:53.000Z","updated_at":"2024-01-04T06:51:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"e64c55fd-e604-4601-962d-29244f7b41a6","html_url":"https://github.com/zhangke3016/ChangeMode","commit_stats":null,"previous_names":["zhangke3016/changemode"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangke3016%2FChangeMode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangke3016%2FChangeMode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangke3016%2FChangeMode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhangke3016%2FChangeMode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhangke3016","download_url":"https://codeload.github.com/zhangke3016/ChangeMode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249986029,"owners_count":21356310,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T01:01:49.604Z","updated_at":"2025-04-21T02:30:40.530Z","avatar_url":"https://github.com/zhangke3016.png","language":"Java","funding_links":[],"categories":["主题样式"],"sub_categories":[],"readme":"#ChangeMode夜间模式开源库\n\u003eImplementation of night mode for Android.\n\n\u003e用最简单的方式实现夜间模式，支持**ListView、RecyclerView**。\n\n\n\n#Preview\n---\n![ChangeMode](gif/screen.gif)\n---\n#Usage xml\n```\n    android:background=\"?attr/zzbackground\"\n    app:backgroundAttr=\"zzbackground\"//如果当前页面要立即刷新，这里传入属性名称  比如R.attr.zzbackground  传zzbackground即可\n    \n    android:textColor=\"?attr/zztextColor\"\n    app:textColorAttr=\"zztextColor\"//如需立即刷新页面效果  同上\n```\n#java\n\n```\n@Override\n    protected void onCreate(Bundle savedInstanceState) {\n    \t\t//1. 在要立即切换效果的页面调用此方法  初始化方法要在super.onCreate方法之前调用 无需在Fragment中重复调用初始化方法\n            ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);\n\t\t\t//在其他页面调用此方法  \n        \t//ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);\n\t\tsuper.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        //添加额外view至夜间管理\n\t\t// ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);\n\t\t//ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent);\n       // ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);\n\n\n       //2. 设置切换\n       //ChangeModeController.changeDay(this, R.style.DayTheme);\n       //ChangeModeController.changeNight(this, R.style.NightTheme);\n    }\n         \n @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        //3. 在onDestroy调用\n        ChangeModeController.onDestory();\n    }\n```\n**关于Fragment**\n```\n@Override\n    public View onCreateView(LayoutInflater inflater, ViewGroup container,\n                             Bundle savedInstanceState) {\n        //use\n        //LayoutInflater.from(getActivity()).inflate(R.layout.fragment_test, container, false)\n\n        //don't use\n       // inflater.inflate(R.layout.fragment_test, container, false);\n        return  LayoutInflater.from(getActivity()).inflate(R.layout.fragment_test, container, false);\n    }\n\n```\n\n#详细操作描述\n---\n##第一步：自定义属性\n```\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cresources\u003e\n                \u003cattr name=\"zzbackground\" format=\"color|reference\"/\u003e\n                \u003cattr name=\"zzbackgroundDrawable\" format=\"reference\"/\u003e\n                \u003cattr name=\"zztextColor\" format=\"color\"/\u003e\n                \u003cattr name=\"zzItemBackground\" format=\"color\"/\u003e\n\u003c/resources\u003e\n\n```\n## 第二步：配置夜间style文件\n```\n\u003cresources\u003e\n\n    \u003c!-- Base application theme. --\u003e\n    \u003cstyle name=\"AppTheme\" parent=\"Theme.AppCompat.Light.DarkActionBar\"\u003e\n        \u003c!-- Customize your theme here. --\u003e\n        \u003citem name=\"colorPrimary\"\u003e@color/colorPrimary\u003c/item\u003e\n        \u003citem name=\"colorPrimaryDark\"\u003e@color/colorPrimaryDark\u003c/item\u003e\n        \u003citem name=\"colorAccent\"\u003e@color/colorAccent\u003c/item\u003e\n       \u003citem name=\"windowActionBar\"\u003efalse\u003c/item\u003e\n        \u003citem name=\"android:windowNoTitle\"\u003etrue\u003c/item\u003e\n        \u003citem name=\"windowNoTitle\"\u003etrue\u003c/item\u003e\n    \u003c/style\u003e\n\n    \u003c!--日间模式 --\u003e\n    \u003cstyle name=\"DayTheme\" parent=\"AppTheme\"\u003e\n        \u003citem name=\"zzbackground\"\u003e@color/dayBackground\u003c/item\u003e\n        \u003citem name=\"zzbackgroundDrawable\"\u003e@drawable/ic_launcher\u003c/item\u003e\n        \u003citem name=\"zztextColor\"\u003e@color/dayTextColor\u003c/item\u003e\n        \u003citem name=\"zzItemBackground\"\u003e@color/dayItemBackground\u003c/item\u003e\n    \u003c/style\u003e\n\n    \u003c!--夜间模式 --\u003e\n    \u003cstyle name=\"NightTheme\" parent=\"AppTheme\"\u003e\n        \u003citem name=\"zzbackground\"\u003e@color/nightBackground\u003c/item\u003e\n        \u003citem name=\"zzbackgroundDrawable\"\u003e@color/nightBackground\u003c/item\u003e\n        \u003citem name=\"zztextColor\"\u003e@color/nightTextColor\u003c/item\u003e\n        \u003citem name=\"zzItemBackground\"\u003e@color/nightItemBackground\u003c/item\u003e\n\n        \u003citem name=\"colorPrimary\"\u003e@color/colorPrimaryNight\u003c/item\u003e\n        \u003citem name=\"colorPrimaryDark\"\u003e@color/colorPrimaryDarkNight\u003c/item\u003e\n        \u003citem name=\"colorAccent\"\u003e@color/colorAccentNight\u003c/item\u003e\n    \u003c/style\u003e\n\n\n    \u003cstyle name=\"AppTheme.AppBarOverlay\" parent=\"ThemeOverlay.AppCompat.Dark.ActionBar\" /\u003e\n    \u003cstyle name=\"AppTheme.PopupOverlay\" parent=\"ThemeOverlay.AppCompat.Light\" /\u003e\n\u003c/resources\u003e\n\n```\n为相关属性设置对应模式的属性值：\n```\n\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cresources\u003e\n    \u003ccolor name=\"dayBackground\"\u003e#F2F4F7\u003c/color\u003e\n    \u003ccolor name=\"dayTextColor\"\u003e#000\u003c/color\u003e\n    \u003ccolor name=\"dayItemBackground\"\u003e#fff\u003c/color\u003e\n\n    \u003ccolor name=\"nightItemBackground\"\u003e#37474F\u003c/color\u003e\n    \u003ccolor name=\"nightBackground\"\u003e#263238\u003c/color\u003e\n    \u003ccolor name=\"nightTextColor\"\u003e#fff\u003c/color\u003e\n\u003c/resources\u003e\n```\n##第三步：在布局文件中配置使用对应属性\n```\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:tools=\"http://schemas.android.com/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n    android:orientation=\"vertical\"\n    android:background=\"?attr/zzbackground\"\n    app:backgroundAttr=\"zzbackground\"\n    tools:context=\"com.thinkfreely.changemode.MainActivity\"\u003e\n\n    \u003candroid.support.design.widget.AppBarLayout\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        app:theme=\"@style/AppTheme.AppBarOverlay\"\u003e\n        \u003candroid.support.v7.widget.Toolbar\n            android:id=\"@+id/toolbar\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"?attr/actionBarSize\"\n            android:background=\"?attr/colorPrimary\"\n            app:backgroundAttr=\"colorPrimary\"\n            app:titleTextColor=\"?attr/zztextColor\"\n            app:popupTheme=\"@style/AppTheme.PopupOverlay\"\n            /\u003e\n    \u003c/android.support.design.widget.AppBarLayout\u003e\n\n        \u003cButton\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"120dp\"\n            android:gravity=\"center\"\n            android:textColor=\"?attr/zztextColor\"\n            app:textColorAttr=\"zztextColor\"\n            android:background=\"?attr/zzItemBackground\"\n            app:backgroundAttr=\"zzItemBackground\"\n            android:padding=\"10dp\"\n            android:layout_marginBottom=\"8dp\"\n            android:textSize=\"22sp\"\n            android:textAllCaps=\"false\"\n            android:text=\"夜间模式切换by Mr.Zk\" /\u003e\n\n    \u003candroid.support.v7.widget.RecyclerView\n        android:id=\"@+id/recyclerView\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"\n        android:scrollbars=\"vertical\"/\u003e\n\u003c/LinearLayout\u003e\n```\n注意**textColorAttr**、**backgroundAttr**、**backgroundDrawableAttr**三个属性。如需当前页面立即刷新，需填加相应属性。\n\n属性|描述\n----|  ------\ntextColorAttr|修改**字体颜色**时设置。如R.attr.zztextColor  传zztextColor 即可。例:app:textColorAttr=\"zztextColor\"\nbackgroundAttr|修改**背景颜色/背景图片**时设置。同上。例: app:backgroundAttr=\"zzbackground\"\nbackgroundDrawableAttr|修改**背景颜色/背景图片**时设置。同上。例: app:backgroundDrawableAttr=\"zzbackground\"\n\n##第四步：页面调用java代码\n```\n@Override\n    protected void onCreate(Bundle savedInstanceState) {\n    \t\t//1. 在要立即切换效果的页面调用此方法\n            ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);\n\t\t\t//在其他页面调用此方法  \n        \t//ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);\n\t\tsuper.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n       //2.设置切换夜间活日间模式\n       //ChangeModeController.changeDay(this, R.style.DayTheme);//切换日间模式\n       //ChangeModeController.changeNight(this, R.style.NightTheme);//切换夜间模式\n    }\n @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        //3.   在onDestroy调用\n        ChangeModeController.onDestory();\n    }\n```\n代码调用**三步**，即可开始夜间之旅。\n如果页面有新创建的视图要加入夜间模式控制，代码调用：\n```\n         //添加额外view至夜间管理\n\t\t// ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);\n\t\t//ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent);\n       // ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);\n```\n如果在改变夜间模式时有其他非标准定义的属性时，可在**ChangeModeController.changeDay**或**ChangeModeController.changeNight**之后调用如下代码给相关属性赋值：\n```\n   TypedValue  attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);\n   toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));\n```\n\n###About me\n---\nAn Android Developer in ZhengZhou.\n\n【[**我的简书地址**](http://www.jianshu.com/users/3c751e06dc32/latest_articles)】\n\n【[**我的CSDN地址**](http://blog.csdn.net/zhangke3016)】\n\n---\n###License\n=======\nCopyright  2016  zhangke\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at \nhttp://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhangke3016%2FChangeMode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhangke3016%2FChangeMode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhangke3016%2FChangeMode/lists"}