Android 应用换肤功能(白天黑夜主题切换)

Stella981
• 阅读 510

有时候使用一些APP的时候发现有一个主题切换的功能,感觉挺好玩的,今天也尝试着做了一下,现在总结换肤经验

_/** _ * 换肤接口 */ public interface ColorUiInterface { View getView();

**void** setTheme(Resources.Theme themeId);

}

2.自定义Reletivelayout控件实现换肤接口

public class ColorRelativeLayout extends RelativeLayout implements ColorUiInterface { private int attr_background = -1; public ColorRelativeLayout(Context context) { super(context); } public ColorRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); this.attr_background = ViewAttributeUtil.getBackgroundAttibute(attrs); } public ColorRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.attr_background = ViewAttributeUtil.getBackgroundAttibute(attrs); } @Override public View getView() { return this; } @Override public void setTheme(Resources.Theme themeId) { if (attr_background != -1) { ViewAttributeUtil.applyBackgroundDrawable(this, themeId, attr_background); } } }

3.在arrs.xml

<**attr** **name=****"colorPrimaryCenter"** **format=****"color|reference"** /> <**attr** **name=****"colorTextIcon"** **format=****"color|reference"** /> <**attr** **name=****"colorPrimaryText"** **format=****"color|reference"** /> <**attr** **name=****"colorSecondText"** **format=****"color|reference"** /> <**attr** **name=****"colorBackground"** **format=****"color|reference"** /> <**attr** **name=****"colorDivider"** **format=****"color|reference"** /> <**attr** **name=****"colorBackgroundAccent"** **format=****"color|reference"** /> <**attr** **name=****"colorHint"** **format=****"color|reference"** />

4.在style里面定义想要的样式主题

<**style** **name=****"BlueTheme"** **parent=****"AppTheme"**> <**item** **name=****"colorPrimary"**>@color/colorBluePrimary</**item**> <**item** **name=****"colorPrimaryDark"**>@color/colorBluePrimaryDark</**item**> <**item** **name=****"colorAccent"**>@color/colorBluePrimaryDark</**item**> _ __ __ _</**style**> <**style** **name=****"RedTheme"** **parent=****"AppTheme"**> <**item** **name=****"colorPrimary"**>@color/colorRedPrimary</**item**> <**item** **name=****"colorPrimaryDark"**>@color/colorRedPrimaryDark</**item**> <**item** **name=****"colorAccent"**>@color/colorRedPrimaryDark</**item**> _ __ __ _</**style**>

5.在Activity中点击按钮弹出对话框(此Activity有两个要求 1.继承

AppCompatActivity 2.实现ColorChooserDialog.ColorCallback)

public void onClick(String content) { new ColorChooserDialog.Builder(this, R.string.theme) .customColors(R.array.colors, null) .doneButton(R.string.done) .cancelButton(R.string.cancel) .allowUserColorInput(false) .allowUserColorInputAlpha(false) .show(); }

  1. @Override public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int selectedColor) { if (selectedColor == ThemeUtils.getThemeColor(this, R.attr.colorPrimary)) return; EventBus.getDefault().post(new SkinChangeEvent()); if (selectedColor == getResources().getColor(R.color.colorBluePrimary)) { setTheme(R.style.BlueTheme); PreUtils.setCurrentTheme(this, Theme.Blue); } else if (selectedColor == getResources().getColor(R.color.colorRedPrimary)) { setTheme(R.style.RedTheme); PreUtils.setCurrentTheme(this, Theme.Red); }}

7.以上更改的是状态栏的主题,修改标题栏样式是这样的

<**com.zcy.ghost.ghost.app.theme.****ColorRelativeLayout** **xmlns:****android****=****"http://schemas.android.com/apk/res/android" ****android****:id=****"@+id/title" ****android****:layout_width=****"match_parent" ****android****:layout_height=****"68dp" ****android****:background=****"?attr/colorPrimary"**> <**TextView ****android****:id=****"@+id/title_name" ****style=****"@style/title_tv_style" ****android****:layout_width=****"match_parent" ****android****:layout_marginTop=****"20dp"** /> </**com.zcy.ghost.ghost.app.theme.****ColorRelativeLayout**>

8.通过调用

ColorUiUtil.changeTheme(rootView, getTheme());

public static void changeTheme(View rootView, Resources.Theme theme) { if (rootView instanceof ColorUiInterface) { ((ColorUiInterface) rootView).setTheme(theme); if (rootView instanceof ViewGroup) { int count = ((ViewGroup) rootView).getChildCount(); for (int i = 0; i < count; i++) { changeTheme(((ViewGroup) rootView).getChildAt(i), theme); } }

}

来通知标题栏回调然后更新background

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
7个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
ES6 新增的数组的方法
给定一个数组letlist\//wu:武力zhi:智力{id:1,name:'张飞',wu:97,zhi:10},{id:2,name:'诸葛亮',wu:55,zhi:99},{id:3,name:'赵云',wu:97,zhi:66},{id:4,na
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
美凌格栋栋酱 美凌格栋栋酱
3星期前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(