博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 应用换肤功能(白天黑夜主题切换)
阅读量:7017 次
发布时间:2019-06-28

本文共 2450 字,大约阅读时间需要 8 分钟。

hot3.png

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

1.

/** * 换肤接口 */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

 

4.在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(); }

 

6.@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.以上更改的是状态栏的主题,修改标题栏样式是这样的

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

转载于:https://my.oschina.net/wlsblogs/blog/1610739

你可能感兴趣的文章
主机和虚拟机能相互ping通但是不能复制
查看>>
angularjs自动化测试系列之jasmine
查看>>
linux 用户打开进程数和文件数调整
查看>>
【小白的CFD之旅】19 来自计算网格的困惑
查看>>
Tcl与Design Compiler (八)——DC的逻辑综合与优化
查看>>
mysql稳定的版本号选择及下载说明(2014-11-10)
查看>>
CSS编写规范
查看>>
[Ubuntu] ubuntu的tty下挂载移动硬盘拷贝数据
查看>>
2017.9.17校内noip模拟赛解题报告
查看>>
英语表达技巧—委婉地表达消极,否定情感
查看>>
无需安装SqlServer打开并管理SqlServer数据库的方法
查看>>
linux上NFS性能参数
查看>>
Linux中基于hadoop安装hive(CentOS7+hadoop2.8.0+hive2.1.1)
查看>>
线程的概念
查看>>
Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化
查看>>
对actuator的管理端点进行ip白名单限制(springBoot添加filter)
查看>>
异步模式
查看>>
MongoDB and GUI 管理界面
查看>>
sqlyog连接Linux上的mysql报错误号码2013,错误号码1130的解决办法
查看>>
获取Android APK JNI库
查看>>