{"id":15027632,"url":"https://github.com/hehonghui/colorful","last_synced_at":"2025-05-16T15:07:10.999Z","repository":{"id":64252216,"uuid":"42176108","full_name":"hehonghui/Colorful","owner":"hehonghui","description":"基于Theme的Android动态换肤库，无需重启Activity、无需自定义View，方便的实现日间、夜间模式。","archived":false,"fork":false,"pushed_at":"2017-08-14T08:32:44.000Z","size":1568,"stargazers_count":1405,"open_issues_count":7,"forks_count":267,"subscribers_count":52,"default_branch":"master","last_synced_at":"2025-05-16T15:07:06.059Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hehonghui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-09T12:09:10.000Z","updated_at":"2025-05-11T18:38:55.000Z","dependencies_parsed_at":"2023-01-15T06:45:39.817Z","dependency_job_id":null,"html_url":"https://github.com/hehonghui/Colorful","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2FColorful","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2FColorful/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2FColorful/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2FColorful/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hehonghui","download_url":"https://codeload.github.com/hehonghui/Colorful/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254553959,"owners_count":22090417,"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-09-24T20:06:49.082Z","updated_at":"2025-05-16T15:07:10.978Z","avatar_url":"https://github.com/hehonghui.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Android Gems](http://www.android-gems.com/badge/bboyfeiyu/Colorful.svg?branch=master)](http://www.android-gems.com/lib/bboyfeiyu/Colorful)\r\n\r\n# Colorful 动态换肤开源库 \r\n\r\n基于Theme的Android动态换肤开源库,以简单的方式实现夜间模式。    \r\n\r\n* 【该方案可以用于切换ui样式，但是还不完善，有需要的同学可自行修改框架扩展自己需要的功能】\r\n\r\n效果如下：\r\n\r\n![](images/colorful.gif)\r\n\r\n\r\n`Demo工程依赖support v7，请自行添加依赖` \r\n\r\n## 一、使用方式\r\n\r\n### 1.1 自定义属性\r\n\r\n```xml\r\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\r\n\u003cresources\u003e\r\n\r\n    \u003c!-- 自定义属性 --\u003e\r\n    \u003cattr name=\"root_view_bg\" format=\"reference|color\" /\u003e\r\n    \u003cattr name=\"btn_bg\" format=\"reference|color\" /\u003e\r\n    \u003cattr name=\"text_color\" format=\"reference|color\" /\u003e\r\n\r\n\u003c/resources\u003e\r\n```\r\n\r\n### 1.2 在布局中使用自定义属性设置View的背景、文本颜色等属性\r\n\r\nactivity_main.xml中的布局: \r\n\r\n\r\n```xml\r\n\u003cRelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\r\n    xmlns:tools=\"http://schemas.android.com/tools\"\r\n    android:id=\"@+id/root_view\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:background=\"?attr/root_view_bg\"\r\n    tools:context=\"com.example.androidthemedemo.MainActivity\" \u003e\r\n\r\n    \u003cTextView\r\n        android:id=\"@+id/textview\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:gravity=\"center\"\r\n        android:text=\"@string/change_theme\"\r\n        android:textColor=\"?attr/text_color\"\r\n        android:textSize=\"20sp\" /\u003e\r\n\r\n    \u003cButton\r\n        android:id=\"@+id/change_btn\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@id/textview\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:text=\"@string/change_theme\"\r\n        android:textColor=\"?attr/text_color\" /\u003e\r\n\r\n    \u003cButton\r\n        android:id=\"@+id/second_btn\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@id/change_btn\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:text=\"@string/sec_act\"\r\n         /\u003e\r\n\r\n    \u003cListView\r\n        android:id=\"@+id/listview\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\"\r\n        android:layout_below=\"@id/second_btn\"\r\n        android:layout_marginTop=\"20dp\" /\u003e\r\n\r\n\u003c/RelativeLayout\u003e\r\n\r\n```\r\n\r\n例如上述布局中我们将root_view的背景设置为`\"?attr/root_view_bg\"`，代表它的背景是自定义属性root_view_bg的值，还有Textview和Button的textColor属性设置为`\"?attr/text_color\"`。\r\n\r\n### 1.3 定义多个Theme\r\n\r\n然后在不同的Theme中为这些属性设置不同的值,例如，通常我们有日间和夜间模式两种颜色模式。styles.xml中的完整代码如下: \r\n\r\n```xml\r\n\u003cresources\u003e\r\n\r\n    \u003cstyle name=\"AppBaseTheme\" parent=\"Theme.AppCompat.Light\"\u003e\r\n    \u003c/style\u003e\r\n\r\n    \u003c!-- Application theme. --\u003e\r\n    \u003cstyle name=\"AppTheme\" parent=\"AppBaseTheme\"\u003e\r\n    \u003c/style\u003e\r\n\r\n    \u003c!-- 日间主题 --\u003e\r\n    \u003cstyle name=\"DayTheme\" parent=\"AppTheme\"\u003e\r\n        \u003citem name=\"root_view_bg\"\u003e@drawable/bg_day\u003c/item\u003e\r\n        \u003citem name=\"btn_bg\"\u003e@color/white_btn_color\u003c/item\u003e\r\n        \u003citem name=\"text_color\"\u003e@color/black_tx_color\u003c/item\u003e\r\n    \u003c/style\u003e\r\n\r\n    \u003c!-- 夜间主题 --\u003e\r\n    \u003cstyle name=\"NightTheme\" parent=\"AppTheme\"\u003e\r\n        \u003citem name=\"root_view_bg\"\u003e@drawable/bg_night\u003c/item\u003e\r\n        \u003citem name=\"btn_bg\"\u003e@color/black_btn_color\u003c/item\u003e\r\n        \u003citem name=\"text_color\"\u003e@color/white_tx_color\u003c/item\u003e\r\n    \u003c/style\u003e\r\n\r\n\u003c/resources\u003e\r\n\r\n```\r\n\r\n两个主题下为同一个属性设置了不同的值，达到切换主题时修改View的相关属性的目的。例如定义在colors.xml中的颜色值。\r\n\r\n```xml\r\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\r\n\u003cresources\u003e\r\n\r\n    \u003c!-- 日间模式 --\u003e\r\n    \u003ccolor name=\"white_btn_color\"\u003e#3BB32E\u003c/color\u003e\r\n    \u003ccolor name=\"black_tx_color\"\u003e#333333\u003c/color\u003e\r\n\r\n    \u003c!-- 夜间模式 --\u003e\r\n    \u003ccolor name=\"black_btn_color\"\u003e#aa7788\u003c/color\u003e\r\n    \u003ccolor name=\"white_tx_color\"\u003e#f0f0f0\u003c/color\u003e\r\n\r\n\u003c/resources\u003e\r\n```\r\n\r\n### 1.4 设置要修改的View的属性\r\n\r\n下面我们为activity_main.xml中的视图进行换肤设置: \r\n\r\n\r\n```java\r\nListView  mNewsListView = (ListView) findViewById(R.id.listview);\r\n\r\n// 为ListView设置要修改的属性,在这里没有对ListView本身的属性做修改\r\nViewGroupSetter listViewSetter = new ViewGroupSetter(mNewsListView, 0);\r\n// 绑定ListView的Item View中的news_title视图，在换肤时修改它的text_color属性\r\nlistViewSetter.childViewTextColor(R.id.news_title, R.attr.text_color);\r\n\r\n\r\n// 构建Colorful对象\r\nColorful mColorful = new Colorful.Builder(this)\r\n\t\t.backgroundDrawable(R.id.root_view, R.attr.root_view_bg) // 设置view的背景图片\r\n\t\t.backgroundColor(R.id.change_btn, R.attr.btn_bg) // 设置按钮的背景色\r\n\t\t.textColor(R.id.textview, R.attr.text_color) // 设置文本颜色\r\n\t\t.setter(listViewSetter)           // 手动设置setter\r\n\t\t.create(); \r\n\r\n```\r\n\r\n\r\n首先我们定义了一个listViewSetter，该Setter用于为ListView的每个Item View中的news_title控件设置文本颜色，文本颜色的值是自定义属性text_color的颜色值。然后构建Colorful对象，并且id分别为change_btn、root_view、textview的控件绑定特定属性值，例如backgroundDrawable(R.id.root_view, R.attr.root_view_bg)代表root_view的背景Drawable为自定义属性root_view_bg的值，textColor(R.id.textview, R.attr.text_color)表示id为textview的TextView控件的文本颜色为R.attr.text_color的值。这些属性都在不同的Theme中有不同的值，因此切换Theme时就会发生变化。然后我们将listViewSetter添加到Colorful对象中，在修改主题时被遍历ListView中的所有Item View，然后修改news_title控件的文本颜色。\r\n\r\n### 1.5 切换主题\r\n最后通过Colorful对象设置主题即可实现切换，代码如下: \r\n\r\n```java\r\n\r\nboolean isNight = false ;\r\n\r\n// 切换主题\r\nprivate void changeThemeWithColorful() {\r\n    if (!isNight) {\r\n        mColorful.setTheme(R.style.DayTheme);\r\n    } else {\r\n\tmColorful.setTheme(R.style.NightTheme);\r\n    }\r\n    isNight = !isNight;\r\n}\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhehonghui%2Fcolorful","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhehonghui%2Fcolorful","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhehonghui%2Fcolorful/lists"}